in reply to Re: here-docs vs quote operators
in thread here-docs vs quote operators

Next, print that anonymous sub to stdout.

If you can do it, tell me how.

Replies are listed 'Best First'.
Re^3: here-docs vs quote operators
by chromatic (Archbishop) on Feb 16, 2006 at 23:52 UTC

    From Perl Hacks:

    my $deparse = B::Deparse->new(); print $deparse->coderef2text( $anonysub );

      Of course this and the Data::Dumper wrapper around this interface won't handle closures properly. (You know this I'm sure, but not everybody else does. :-) Which is why using Data::Dump::Streamer is IMO preferable.

      ---
      $world=~s/war/peace/g

Re^3: here-docs vs quote operators
by demerphq (Chancellor) on Feb 17, 2006 at 10:24 UTC

    Use Data::Dump::Streamer to do the deparsing for you. (This is also mentioned in perl-hacks I think :-)

    use Data::Dump::Streamer; my $hd = sub { '[]' =~ /\\[\\]/ }; Dump($hd)->Names('hd')->Out(); __END__ $hd = sub { '[]' =~ /\\[\\]/; };
    ---
    $world=~s/war/peace/g

Re^3: here-docs vs quote operators
by Corion (Patriarch) on Feb 16, 2006 at 21:48 UTC

    How about this?

    use strict; use Data::Dumper; sub print_code { my ($sub) = @_; my $d = Data::Dumper->new( [$sub] ); $d->Deparse(1); print $d->Dump(); }; print_code(sub{ print 'This is a coderef' });
      Hmm... doesn't work on my system. :-( I get: Can't locate object method "Deparse" via package "Data::Dumper" (perhaps you forgot to load "Data::Dumper"?) at - line 6.

      What version of perl and/or Data::Dumper do I need to pull off that trick? :-(

        It works for me with Data::Dumper 2.121, on v5.8.5 built for MSWin32-x86-multi-thread. There is no other version of Data::Dumper on CPAN and I think that all the 5.8 Perls come with this feature.