I noticed a fundamental difference between literal scalars and hashes/arrays.
use strict; use warnings; sub pr { print \$_[0],"\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"; for (1..3) { pr 1; } print "\n"; } SCALAR(0x81953b0) SCALAR(0x8195398) SCALAR(0x8195374) SCALAR(0x8195398) SCALAR(0x8195374) SCALAR(0x819538c) SCALAR(0x8195398) SCALAR(0x819538c) SCALAR(0x8195398) SCALAR(0x81953b0) SCALAR(0x819538c) SCALAR(0x81953b0) --- Array for (1..3) { pr [1,2]; print "\n\t"; for (1..3) { pr [1,2]; } print "\n"; } REF(0x819073c) REF(0x8190724) REF(0x8190724) REF(0x8190724) REF(0x819073c) REF(0x8190724) REF(0x8190724) REF(0x8190724) REF(0x819073c) REF(0x8190724) REF(0x8190724) REF(0x8190724) --- Hash for (1..3) { pr {1,2}; print "\n\t"; for (1..3) { pr {1,2}; } print "\n"; } REF(0x8195374) REF(0x8190748) REF(0x8190748) REF(0x8190748) REF(0x81953c8) REF(0x8190748) REF(0x8190748) REF(0x8190748) REF(0x8195374) REF(0x8190748) REF(0x8190748) REF(0x8190748)
As you can see the ref of the arrays and hashes depend on the codeposition while the refs of the scalars are not ...
my motivation is to find a way to distinguish different calls to the same sub by the codeposition.
(please note that caller gives only the line number!)
QUESTION: Is this a defined behaviour, and why does the compiler not just send one stable ref too a scalar?
Cheers Rolf
UPDATE: changed intendation of inner loop and added question!
UPDATE: OK there is no need to discuss this further.
as an outcome it is clear that perl may reallocate memory for data which is obviously constant at compiletime. In other words, the reference to a constant or literal at a special codeposition may always change during runtime and is not fixed at compiletime
In reply to Reference of constants and literals by LanX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |