Yes, exactly!

After some thought, I need better (or just more) terminology.

Let us say that you use the \ and -> operators with "Perl references". Then let us say that Perl itself makes heavy use of a type of reference internally but we'll call these internal references "pointers" since that is probably less confusing for this discussion. Now a "pointer" isn't what a C programmer would call a pointer since there is at least one more layer of abstraction that isn't important for this discussion (so a "pointer" contains a C pointer plus a few other things).

So a Perl scalar variable is really a name for a "pointer" to a scalar value (called an SV in the source code). So $x points to some SV structure that contains $x's flags saying what type(s) of value(s) $x currently has and contains those values.

And a Perl array is an array of "pointers" to SVs. So calling test(@v) copies the "pointers" from @v to @_ so we end up with $v[0] and $_[0] pointing to the same SV structure and changing a value via one also changes the value seen via the other. The two are said to be aliases of each other.

But doing @_= @v causes new SV structs to be allocated for @_ and the values from @v's SVs are copied to @_'s new SVs.

And so doing "print $x" involves an "automatic dereference" of $x's "pointer".

Now a "Perl reference" is a(n) SV that contains a "pointer".

You can watch aliases in work via:

sub refd { print join( " ", \(@_) ), $/; } my @v= qw( a b c ); print "@v: ", join( " ", \(@v) ), $/; refd( @v ); refd( $v[0] ); refd( @v[1,2,0] ); refd( "$v[1]" );
You'll note that you get the same references except in the last case.

        - tye (but my friends call me "Tye")

In reply to (tye)Re2: Calling subroutine in a package from other perl sub. by tye
in thread Calling subroutine in a package from other perl sub. by Anonymous Monk

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.