I think I've covered that one in &assignTo by doing @_ = @temp; but I've thought of another couple of variations. Here's a revised script

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}; assignToRef(@arr); print qq{ Contents now @arr\n}; listArgs(@arr); print qq{ Contents now @arr\n}; loopOver(@arr); print qq{ Contents now @arr\n}; mapOver(@arr); print qq{ Contents now @arr\n}; refToArray(@arr); print qq{ Contents now @arr\n}; refToElems(@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 assignToRef { my @temp = @_; my $raARG = \@_; print qq{In assignToRef - assign to ref. to \@_\n}; map { $_ ++ } @temp; @$raARG = @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 mapOver { print qq{In mapOver - increment \@_ in map\n}; map { $_ ++ } @_; } sub refToArray { my $raARG = \@_; print qq{In refToArray - take reference to \@_ array\n}; $raARG->[0] ++; $raARG->[1] ++; } sub refToElems { my ($rsVar1, $rsVar2) = \(@_); print qq{In refToElems - 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] ++; }

and output

At initialisation Contents are 1 2 In assignTo - assign to @_ Contents now 1 2 In assignToRef - assign to ref. to @_ Contents now 1 2 In listArgs - list of lexicals Contents now 1 2 In loopOver - loop over @_ Contents now 2 3 In mapOver - increment @_ in map Contents now 3 4 In refToArray - take reference to @_ array Contents now 4 5 In refToElems - take reference to @_ elements Contents now 5 6 In shiftArgs - lexicals via shift Contents now 5 6 In useDirect - increment elements directly Contents now 6 7

Given more time I'd refactor to use an array of code refs. and test in a loop as the script is getting quite long but I'm rushing to get ready for a holiday so it'll have to stay as it is.

Cheers,

JohnGG


In reply to Re^5: 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.