Printing out the code of an anonymous subroutine

前端 未结 4 1907
挽巷
挽巷 2021-02-05 12:56

I\'m currently working in a very complex Perl architecture, and I want to create some debugging tools. Since a lot of the behavior involves anonymous subroutines, I\'d like to a

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-05 13:29

    Yeah, Data::Dumper can be told to bring in B::Deparse, via something like:

    #!/usr/bin/perl
    
    use Data::Dumper;
    use strict;
    use warnings;
    $Data::Dumper::Deparse = 1;
    
    my $code = sub { my $a = 42;  print $a ** 2; };
    
    print Dumper $code;
    

    There is an object-oriented interface as well (described in the perldoc for Data::Dumper), if you prefer.

    Note: The code that is output won't be identical to what you originally specified, but it will have the same semantics.

提交回复
热议问题