Monday, August 10, 2015

how to debug anonymous functions and coderefs in Perl 5

http://perldoc.perl.org/B/Deparse.html


use strict;
use warnings;
use B::Deparse;

sub debug_coderef {
    my $possibly_code = shift; 
    my $deparse = B::Deparse->new();
    if (ref $possibly_code && ref $possibly_code eq 'CODE') {
        print STDERR "CODEREF: ", $deparse->coderef2text($possibly_code), "\n";
    }  else {
        print STDERR "NOT A CODEREF: ", Dumper($possibly_code), "\n";
    }
}

debug_coderef(sub {print "a";});