http://qs321.pair.com?node_id=909229

saberworks has asked for the wisdom of the Perl Monks concerning the following question:

This is a weird question but I'm trying to track down a problem with Dancer's new "hooks" functionality. Adding a hook in Dancer essentially stores a list of code refs (subroutine references) in a list and then executes them in a loop like this:
$_->(@args) foreach @{$self->get_hooks_for($hook_name)};
The problem I'm having is that in a persistent environment (under plackup, where my modules aren't reloaded on each web request) in this list of code references one of them somehow goes undef during execution so that upon the next request Dancer dies with a runtime error (because $_ is undef and it's trying to call $_->(@args)).

If I use Data::Dumper just before and just after the line above I get this:
$VAR1 = [ sub { "DUMMY" }, sub { "DUMMY" } ];
after:
$VAR1 = [ sub { "DUMMY" }, undef ];
If I change the original line in the Dancer module to this the error goes away:
foreach my $hook (@{ $self->get_hooks_for($hook_name) }) { $hook->(@args); }
This is really odd to me because I can't think of a way executing a coderef will turn it into undef. It seems like some issue with $_ getting overwritten somehow but I don't know how that could happen. I tried to repro this on a blank project by setting $_ to all sorts of values but wasn't able to.

Dancer (or plackup?) has an annoying habit of hiding errors in these hooks (If I explicitly call die('foo') it silently continues and 'foo' is lost in the ether, not showing up in the response or any error logs) but I did try to die inside the code ref to see if I could repro it.

Update: If I pull out the hooks separately ahead of time like this, it also fixes the bug:
my @hooks = @{ $self->get_hooks_for($hook_name) }; $_->(@args) foreach @hooks;
The "get_hooks_for" method does this: $self->hooks->{$hook_name} || []; And the "hooks" method is magically created like this: __PACKAGE__->attributes(qw/ hooks registered_hooks/); Thanks for any help.

Replies are listed 'Best First'.
Re: Is there something that can turn a coderef undef from inside the coderef?
by BrowserUk (Patriarch) on Jun 10, 2011 at 23:51 UTC

    Like this maybe?

    sub a{ print 'from a'; undef $_ };; sub b{ print 'from b'; undef $_ };; @r = ( \&a, \&b );; $_->( 1 ) for @r;; from a from b $_->( 1 ) for @r;; Use of uninitialized value in subroutine entry at (eval 13) Undefined subroutine &main:: called at (eval 13)

    Basically $_ becomes an alias to the stored coderef. If the code called assigns to $_ without localising it, the coderef will be overwritten.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Is there something that can turn a coderef undef from inside the coderef?
by AnomalousMonk (Archbishop) on Jun 11, 2011 at 00:10 UTC
    Update: If I pull out the hooks separately ahead of time like this, it also fixes the bug ...

    More support for BrowserUk's theory: If  $_ gets singed during all the  get_hooks_for() fireworks, it has no effect on the subsequent topicalization of  $_ in the  foreach loop.

Re: Is there something that can turn a coderef undef from inside the coderef?
by saberworks (Curate) on Jun 12, 2011 at 00:05 UTC
    Thanks, this resulted in a fix.
      When you're thanking someone, wouldn't it make more sense to reply to their node, rather than to your own?
Re: Is there something that can turn a coderef undef from inside the coderef?
by Anonymous Monk on Jun 13, 2011 at 12:42 UTC
    "Holy EQUIVALENCE, Captain Fortran!"