in reply to Avoiding reference of sub optimization

UPDATE: I'm still not sure if this should be considered a feature or a bug. It's not the same as with other "data" structures:
A subroutine isn't a data structure. Note that the same happens with other literals:
#!/usr/bin/perl -l sub gen_arr { my $inner =shift; print my $outer = \"foo"; return $outer; } gen_arr gen_arr; __END__ SCALAR(0x9400bc4) SCALAR(0x9400bc4)
If you really must create different addresses, use eval:
my $outer = eval "sub {1;}"; # Different address each time.

Replies are listed 'Best First'.
Re^2: Avoiding reference of sub optimization
by LanX (Saint) on Mar 21, 2011 at 15:30 UTC
    Just for clarity!

    It's not the literal which makes the difference but - as already suspected in the OP - the constant folding.

    #!/usr/bin/perl -l sub gen_arr { my $inner =shift; print my $outer = \"foo$inner"; # still a literal but not constant a +nymore return $outer; } gen_arr gen_arr;

    Use of uninitialized value $inner in concatenation (.) or string at /t +mp/tst2.pl line 6. SCALAR(0x912c760) SCALAR(0x912c9d0)

    Cheers Rolf

Re^2: Avoiding reference of sub optimization
by LanX (Saint) on Mar 21, 2011 at 15:05 UTC
    > A subroutine isn't a data structure.

    Well obviously not in Perl, but there is a reason why I've put "data" into quotes. :)

    But thanks for the literal example, good point! ¹

    Cheers Rolf

    UPDATE:

    1) and another example of constant folding.