Strange things are occurring at compile time
David Cantrell
david at cantrell.org.uk
Thu Mar 1 15:40:17 GMT 2012
On Thu, Mar 01, 2012 at 10:39:59AM +0000, Matt Lawrence wrote:
> 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?
Sort of. But note that my code had an extra wrinkle - I was also
looking at the subroutine reference *at compile time* as well as later
at runtime, and the same reference was showing up with two different
addresses.
But you're right, it was caused by the subroutine being a closure - Matt
Trout pointed that out on IRC, earning himself a delicious pint of ale
this evening.
--
David Cantrell | semi-evolved ape-thing
Irregular English:
ladies glow; gentlemen perspire; brutes, oafs and athletes sweat
More information about the london.pm
mailing list