Is there a simple explanation for this?

References be tricky, so I don't know how "simple" you will find this, but however...

c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(pp); ;; sub clear_it { my($data) = @_; print 'in clear_it(): A: ref address: ', $data; $data = []; print 'in clear_it(): B: ref address: ', $data; } ;; my $data = ['a','b']; print 'in main: X: ref address: ', $data; ;; clear_it($data); print 'in main: Y: ref address: ', $data; print 'in main: Z: ref content: ', pp $data; " in main: X: ref address: ARRAY(0x15c6f3c) in clear_it(): A: ref address: ARRAY(0x15c6f3c) in clear_it(): B: ref address: ARRAY(0x15c7074) in main: Y: ref address: ARRAY(0x15c6f3c) in main: Z: ref content: ["a", "b"]
In this first example, the anonymous reference address created in main and assigned to $data in that scope is passed to a separate variable in the scope of clear_it(), and that separate variable is assigned another anonymous reference address created therein. Notice how the the reference addresses change from point A to point B, yet are the same at points X, A and Y. After a new reference address is assigned to $data within clear_it(), whatever is done to the referenced contents (the referent) of $data inside of clear_it() can have no effect on the referent of the separate $data variable in main.

c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(pp); ;; sub clear_it { my($data) = @_; print 'in clear_it(): A: ref address: ', $data; @$data = (); print 'in clear_it(): B: ref address: ', $data; } ;; my $data = ['a','b']; print 'in main: X: ref address: ', $data; ;; clear_it($data); print 'in main: Y: ref address: ', $data; print 'in main: Z: ref content: ', pp $data; " in main: X: ref address: ARRAY(0x1846f3c) in clear_it(): A: ref address: ARRAY(0x1846f3c) in clear_it(): B: ref address: ARRAY(0x1846f3c) in main: Y: ref address: ARRAY(0x1846f3c) in main: Z: ref content: []
In this example, the reference address of an anonymous array in main is again passed to clear_it(), but this time an operation is performed by reference on the referent of the original reference, specifically, assigning it the empty list. Notice that the reference addresses at points X, A, B and Y are all the same.

This seems like a perl idiosyncrasy.

This is essentially the way references work in any language.


Give a man a fish:  <%-{-{-{-<


In reply to Re: Pass array, then clear by AnomalousMonk
in thread Pass array, then clear 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.