First of all it is dangerous because the possibility for confusion is high. Sure, you may know to do it only when the called subroutine is unwilling to take arguments, but will the person who learns from your code know that?

As for your cute test function, it is really simple. Perl scalars are always references to data, not the actual data. So if you want to convert a list of scalars into a list, it is substantially more efficient to just make a list of pointers to the data than really copy. And that is what Perl does when passing data into a function. However when you assign in Perl you assign by value, not reference.

So your 3 variables coming in are really passed in by reference, so in test2 you are able to change the original variables (whether the originals were from a scalar or an array) through the references. (ie @_ is just a list of pointers back to the original variables.) In test1 you threw away the three references and then assigned to new variables. Well if you no longer have the three variables, then assigning to new ones won't change a thing.

And as you note this works just as well if you have the original argument list being 3 variables or an array with 3 things.

So the rule is that assignment is by value, and arguments are passed into functions by reference. You probably understand this correctly if you can puzzle out the following example:

($x, $y, $z) = 'x'..'z'; print "Original values.\n"; show_vals(); print "This rotates them.\n"; rotate($x, $y, $z); show_vals(); print "Why doesn't this?\n"; rotate_not($x, $y, $z); show_vals(); sub rotate { @_[-1..($#_-1)] = @_; } sub rotate_not { @_ = @_[-1..($#_-1)]; } sub show_vals { print "\$x: $x\n\$y: $y\n\$z: $z\n\n"; }

In reply to Re (tilly) 4: Calling subroutine in a package from other perl sub. by tilly
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.