Strange things are occurring at compile time

Matt Lawrence matt.lawrence at virgin.net
Thu Mar 1 10:39:59 GMT 2012


Anonymous subroutines without references to external variables that have 
identical bodies are identical subroutines.

$ perl -le 'push @subs, sub { print "Hello" } for 0 .. 3; print for @subs'
CODE(0x94be884)
CODE(0x94be884)
CODE(0x94be884)
CODE(0x94be884)

$ perl -le 'for my $value (0 .. 3) { push @subs, sub { print $value } } 
print for @subs'
CODE(0x9204254)
CODE(0x9214138)
CODE(0x9214a70)
CODE(0x9214afc)

Is this what's causing the same ref to be seen twice?

Matt

On 29/02/12 17:00, David Cantrell wrote:
> I've got some strange things going on in my code, and it's DOING MY 'EAD
> IN.
>
> In a parent class I have:
>
>    sub MODIFY_CODE_ATTRIBUTES {
>      my ($package, $subref, @attrs) = @_;
>      use Data::Dumper;local $Data::Dumper::Indent=1;
>      warn("storing attribs for $subref: ".join(", ", @attrs)."\n");
>      $attrs{ refaddr $subref } = \@attrs;
>      return;
>    }
>
> and then in my class:
>
>    my $wibble = sub :Something :AnotherThing {
>      ... long subroutine body ...
>    }
>    warn("defined $wibble\n");
>
> As it compiles, it wibbles about storing the attribs, with a stringified
> coderef.  Then at runtime it warn()s that it has defined something *with
> a different coderef*:
>
>    storing attribs for CODE(0x55f5528): Something, AnotherThing
>    defined CODE(0x54245c8)
>
> but if I comment out the subroutine body I get the same code-ref twice.
>
> Am I going mad here?  I've already checked the obvious things - yes, my
> code compiles *and runs* properly without any of this attribute nonsense
> so there's no missing braces or anything; there are no eval { blocks }
> or 'use's in the subroutine I'm defining that might Dick About With
> Things at compile-time.
>
> My apologies for the off-topic post.  Let me rehabilitate myself by
> offering FREE BOOZE at tomorrow's social to anyone who can help me.
>



More information about the london.pm mailing list