View on
Ben Morrow > Clone-Closure > Clone::Closure



Annotate this POD


New  1
Open  1
View/Report Bugs
Module Version: 0.06   Source  


Clone::Closure - A clone that knows how to clone closures


    use Clone::Closure qw/clone/;

    my $total;

    sub count {
        my $count;
        return sub { $count++, $total++ };

    my $foo = count;
    my $bar = clone $foo;

    # $bar has its own copy of $count, but shares $total 
    # with $foo.


This module provides a clone method which makes recursive copies of nested hash, array, scalar and reference types, including tied variables, objects, and closures.

clone takes a scalar argument. To duplicate arrays or hashes, pass them in by reference, e.g.

    my $copy = clone \@array;

    # or

    my %copy = %{ clone \%hash };

Values which are not cloned

Sub (except for "Closures"), glob, format and IO refs are simply duplicated, not cloned.


Closures are cloned, unlike with Clone. Closed-over lexicals will be cloned if they were originally declared in a scope that could be run more than once, and shared otherwise.

That is, in the example in the "SYNOPSIS", $count is cloned as it is scoped to &count, which can run many times with different $count variables; but $total is shared as it is file-scoped, so there will only ever be one copy.

Generally speaking, clone will produce what might have been another copy of the closure, generated by the same means. However, see "BUGS" below.


The following types of magic are preserved:

All other types of magic are dropped when cloning, so for example

    my $env = clone \%ENV;

will produce a normal hashref containing a copy of the environment.



Loops are currently not correctly recognized as 'scopes that may run more than once'. That is, given

    my @subs;

    for my $i (1..10) {
        push @subs, sub { $i };

a clone of $subs[0] will share $i, which is probably not what you wanted. One possible workaround is to generate the closure in a sub, with its own lexical; for example

    my @subs;

    sub make_closure {
        # this is important, so we get a new lexical
        my $i = shift;
        return sub { $i };

    for my $i (1..10) {
        push @subs, make_closure $i;

A clone of $subs[0] will now have its own copy of $i.

Note that this behaviour will change in a future release; unfortunately, I can't provide a warning (as I haven't worked out how to detect loops...).

5.6 and eval STRING

Under 5.6, lexicals which are closed over by eval STRING will always be cloned, never shared. That is, given

    my $x;
    my $sub = eval 'sub { $x }';

a clone of $sub will have its own copy of $x, which is incorrect.



This module is based on Clone v0.23 by Ray Finch, <>.

Clone is copyright 2001 Ray Finch.

This module is copyright 2007 Ben Morrow, <>.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Clone, Storable.

syntax highlighting: