meta4 has asked for the wisdom of the Perl Monks concerning the following question:

I have wanted to dump closures for a while, but it always appeared to be impossible. For example I looked at the Caveats Dumping Closures from Data-Dump-Streamer, and it scared me off.

But, today I played around with B-Deparse and PadWalker and came up with a dump_closure function that serializes a closure into a reasonable stringified version of the closure. It can even be eval'd to recreate the closure. So, I'm wondering what traps I'm not seeing?

Here is the code along with some test cases.

use warnings; use strict; use B::Deparse; use PadWalker qw( closed_over ); sub dump_closure { my $f = shift; # the closure my $conf = shift || (); # hashref of options my $indent = exists $conf->{indent} ? $conf->{indent} : ' '; my @deparse_opts = exists $conf->{deparse_opts} ? @{$conf->{deparse_opts}} : ('-sC'); my ( $lexical_vars ) = closed_over( $f ); my @lexical_vars = map { $indent . 'my '. $_. ' = '. ${$lexical_vars->{$_}}. ';' } sort keys %$lexical_vars; my $bd = B::Deparse->new( @deparse_opts ); $bd->ambient_pragmas( strict => 'all', warnings => 'all' ); my @body = split /\n/, $bd->coderef2text( $f ); $body[0] = 'return sub ' . $body[0]; @body = map { $indent . $_ } @body; return join "\n", 'sub {', @lexical_vars, @body, '} -> ()'; } ###################################################################### +######## # 2 * 3 * 4 = 24 = mult_n_n_n(2)->(3)->(4) sub mult_n_n_n { # nested closure my $a = shift; return sub { my $b = shift; return sub { my $c = shift; return $a * $b * $c; } } } my $mult_2_n_n = mult_n_n_n( 2 ); my $mult_2_3_n = $mult_2_n_n->( 3 ); print dump_closure( $mult_2_n_n ), ";\n"; print dump_closure( $mult_2_3_n ), ";\n"; my $copy_2_3_n = eval( dump_closure( $mult_2_3_n ) ); print $@ if $@; print $copy_2_3_n->( 4 ), "\n"; # clone works my $copy_2_n_n = eval( dump_closure( $mult_2_n_n ) ); print $@ if $@; print $copy_2_n_n->( 3 )->( 4 ), "\n"; #clone works

This produces:

sub {
    my $a = 2;
    return sub {
        my $b = shift @_;
        return sub {
            my $c = shift @_;
            return $a * $b * $c;
        }
        ;
    }
} -> ();
sub {
    my $a = 2;
    my $b = 3;
    return sub {
        my $c = shift @_;
        return $a * $b * $c;
    }
} -> ();
24
24

How do I get this functionality in Data-Dump or Data-Dump-Streamer or the debugger?

Replies are listed 'Best First'.
Re: How to dump closures
by Corion (Patriarch) on May 04, 2010 at 06:58 UTC

    Data::Dumper supports at least subroutines and I think also closures by use of B::Deparse. I'm not sure how comprehensive the dumping will be if you dump several closures that close over the same variables, but that's a problem shared by all dumpers.

      But, that is what I think I've solved here. With this method I can close over the same variables as many times as I want and all the closed over variables are dumped in separate lexical spaces; they keep their original name; and they don't pollute the enclosing names space.
Re: How to dump closures
by Anonymous Monk on May 04, 2010 at 05:50 UTC
    d00d, has you gotz any ideas how slows this code be ? let me tells you how slows, dead-snail mail slow !