Skip to content

Instantly share code, notes, and snippets.

@towhans
Created June 22, 2015 11:07
Show Gist options
  • Save towhans/4cb3ed739681b867a01c to your computer and use it in GitHub Desktop.
Save towhans/4cb3ed739681b867a01c to your computer and use it in GitHub Desktop.
Debug AnyEvent code with this one simple trick
# #############################################################
# Paste the following code in your program.
# It will redefine AE::cv globally.
# You'll be able to pause in-between individual closures
# as they are called by the event loop. You'll
# also see the source code of the closure being called.
#
# Credits:
# Brian D. Foy: http://www.effectiveperlprogramming.com/2011/09/enchant-closures-for-better-debugging-output/
# #############################################################
my $aecv = \&AE::cv;
*AE::cv = sub (;&) {
my $fun = \&{shift @_};
require B;
my $gv = B::svref_2object($fun)->GV;
require B::Deparse;
my $deparse = B::Deparse->new;
my $text = $deparse->coderef2text($fun);
my $fun2 = sub {
print "Next: $text\n";
print "Enter to resume...";
<STDIN>;
$fun->(@_);
};
&$aecv( $fun2 );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment