LanX has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Reference of constants and literals
by moritz (Cardinal) on Nov 24, 2008 at 10:09 UTC | |
by Corion (Patriarch) on Nov 24, 2008 at 10:19 UTC | |
by moritz (Cardinal) on Nov 24, 2008 at 10:29 UTC | |
by ikegami (Patriarch) on Nov 24, 2008 at 11:00 UTC | |
by JavaFan (Canon) on Nov 24, 2008 at 11:37 UTC | |
| |
by LanX (Saint) on Nov 24, 2008 at 10:46 UTC | |
|
Re: Reference of constants and literals
by ikegami (Patriarch) on Nov 24, 2008 at 10:37 UTC | |
|
Re: Reference of constants and literals
by ikegami (Patriarch) on Nov 24, 2008 at 10:18 UTC | |
by LanX (Saint) on Nov 24, 2008 at 10:30 UTC | |
by ikegami (Patriarch) on Nov 24, 2008 at 10:44 UTC | |
by LanX (Saint) on Nov 24, 2008 at 11:11 UTC | |
by ikegami (Patriarch) on Nov 24, 2008 at 11:30 UTC | |
| |
by moritz (Cardinal) on Nov 24, 2008 at 10:34 UTC | |
by LanX (Saint) on Nov 24, 2008 at 11:28 UTC | |
by ikegami (Patriarch) on Nov 24, 2008 at 11:49 UTC | |
| |
by LanX (Saint) on Nov 24, 2008 at 11:35 UTC | |
by Anonymous Monk on Nov 25, 2008 at 08:33 UTC |