Hi

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.