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?


In reply to How to dump closures by meta4

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.