Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: Global symbol requires explicit package name

by ColonelPanic (Friar)
on Nov 28, 2012 at 16:43 UTC ( #1006071=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Global symbol requires explicit package name
in thread Global symbol requires explicit package name

Yeah, but if you are only passing a couple of scalars, along with a single array (as in the original sub design), then passing by reference is not necessarily superior. It depends on what you are doing. Not passing by reference allows for more flexible calls to the sub:

scalar_and_array_sub($foo, $bar, @values[1..5]); scalar_and_array_sub($foo, $bar, $value1, $value2, $value3); scalar_and_array_sub($foo, $bar, split(/\s/, $string));

With pass by reference, these would have messier syntax.



When's the last time you used duct tape on a duct? --Larry Wall


Comment on Re^4: Global symbol requires explicit package name
Download Code
Re^5: Global symbol requires explicit package name
by greengaroo (Hermit) on Nov 28, 2012 at 18:34 UTC

    Yes, but you can still use an ArrayRef, with the square brackets:

    scalar_and_array_sub($foo, $bar, [@values[1..5]]); scalar_and_array_sub($foo, $bar, [$value1, $value2, $value3]); scalar_and_array_sub($foo, $bar, [split(/\s/, $string)]);
    But I agree with you, if you don't pass too many arguments, you should use an Array instead of an ArrayRef.

    Testing never proves the absence of faults, it only shows their presence.
      The first example doesn't provide equivalent functionality, however. When an array slice is passed normally, modifying @_ will modify the original array. But this makes an anonymous copy of the array slice, which is discarded after the function call.


      When's the last time you used duct tape on a duct? --Larry Wall

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006071]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (13)
As of 2014-09-22 12:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (191 votes), past polls