I wanted to understand what was happening, what affected the caller and what didn't so I put a short script together. I may have missed ways to get at @_ and I would be interested if any omissions were pointed out.

use strict; use warnings; my @arr = (1, 2); print qq{At initialisation\n Contents are @arr\n}; assignTo(@arr); print qq{ Contents now @arr\n}; listArgs(@arr); print qq{ Contents now @arr\n}; loopOver(@arr); print qq{ Contents now @arr\n}; refTo(@arr); print qq{ Contents now @arr\n}; shiftArgs(@arr); print qq{ Contents now @arr\n}; useDirect(@arr); print qq{ Contents now @arr\n}; sub assignTo { my @temp = @_; print qq{In assignTo - assign to \@_\n}; map { $_ ++ } @temp; @_ = @temp; } sub listArgs { my ($var1, $var2) = @_; print qq{In listArgs - list of lexicals\n}; $var1 ++; $var2 ++; } sub loopOver { print qq{In loopOver - loop over \@_\n}; $_ ++ for @_; } sub refTo { my ($rsVar1, $rsVar2) = \(@_); print qq{In refTo - take reference to \@_ elements\n}; $$rsVar1 ++; $$rsVar2 ++; } sub shiftArgs { my $var1 = shift; my $var2 = shift; print qq{In shiftArgs - lexicals via shift\n}; $var1 ++; $var2 ++; } sub useDirect { print qq{In useDirect - increment elements directly\n}; $_[0] ++; $_[1] ++; }

The output produced is

At initialisation Contents are 1 2 In assignTo - assign to @_ Contents now 1 2 In listArgs - list of lexicals Contents now 1 2 In loopOver - loop over @_ Contents now 2 3 In refTo - take reference to @_ elements Contents now 3 4 In shiftArgs - lexicals via shift Contents now 3 4 In useDirect - increment elements directly Contents now 4 5

A very interesting topic that has cleared up some misunderstandings.

Cheers,

JohnGG


In reply to Re^3: Why does assignment change the result? by johngg
in thread Why does assignment change the result? 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.