in reply to Re^4: Reference of constants and literals
in thread Reference of constants and literals

Push is a CORE:: subroutine which you can override, please check perlsub

This is off topic. I wanted to ignore the push example to avoid going off topic.

push is a named operator, which Perl calls "built-in function" or just "function". There are number of differences between subroutines and functions.

As for CORE::push, it's very special. It's both a subroutine and a function depending how it's used. \&CORE::push returns a code ref, so it acts as a subroutine. But CORE::push(...) results in a push operator, not a subroutine call.

well the constant gets always at runtime a reallocated space, while the arrayref doesn't!!!

You haven't shown that the array ref doesn't get reallocated. To do so, you'd have to show that the array doesn't happen to get reallocated at the same memory address. All you have is a theory. One that's provably false.

my $code= <<"__EOC"; my \@x; <-- added for (1..3) { $_call_ print "\\n\\t"; for (1..3) { $_call_ push \@x, 'x'; <-- added } print "\\n"; } __EOC
--- Array my @x; for (1..3) { pr [1,2]; print "\n\t"; #UPDATE for (1..3) { pr [1,2]; push @x, 'x'; } print "\n"; } REF(0x18538d8) REF(0x18538c0) REF(0x185389c) REF(0x1853950) REF(0x185389c) REF(0x1853914) REF(0x1853920) REF(0x18538a8) REF(0x1853920) REF(0x1853884) REF(0x1853890) REF(0x185386c)

And that brings me back to my earlier answer. The behaviour is not intentional. It's a fluke and relying on it is dangerous.

Replies are listed 'Best First'.
Re^6: Reference of constants and literals
by LanX (Saint) on Nov 24, 2008 at 16:34 UTC
    Thank you for this example, the following one makes it even more obvious!
    use strict; use warnings; sub pr { my $ref= ref($_[0]) ? $_[0] : \$_[0]; print $ref,"\t"; # print ref of para } check(qw/ Scalar 1 /); check(qw/ Array [1,2] /); check(qw/ Hash {1,2} /); sub check { my ($_title_,$type) = @_; my $_call_ ="pr $type;"; my $code= <<"__EOC"; for (1..3) { $_call_ print "\\n\\t"; #UPDATE for (1..3) { $_call_ } print "\\n"; } __EOC print "\n--- $_title_ \n"; print $code; eval $code; }
    OUTPUT
    --- Scalar for (1..3) { pr 1; print "\n\t"; #UPDATE for (1..3) { pr 1; } print "\n"; } SCALAR(0x818ad04) SCALAR(0x818acec) SCALAR(0x818acf8) SCALAR(0x818ac +ec) SCALAR(0x818acf8) SCALAR(0x818ad10) SCALAR(0x818acec) SCALAR(0x818ad +10) SCALAR(0x818acec) SCALAR(0x818ad04) SCALAR(0x818ad10) SCALAR(0x818ad +04) --- Array for (1..3) { pr [1,2]; print "\n\t"; #UPDATE for (1..3) { pr [1,2]; } print "\n"; } ARRAY(0x818acf8) ARRAY(0x818acd4) ARRAY(0x818acd4) ARRAY(0x818acd +4) ARRAY(0x818acd4) <----- FAILS ARRAY(0x818acf8) ARRAY(0x818acf8) ARRAY(0x818acf +8) ARRAY(0x818acf8) ARRAY(0x818acd4) ARRAY(0x818acd4) ARRAY(0x818acd +4)
    Thanks for teh discussion!!!

    Cheers Rolf