Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Global symbol requires explicit package name

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


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

I agree that the original code is not clean. However, your solution makes an unnecessary copy of the array. You could just do this

my $fh=shift; my $h=shift; foreach my $key (@_)

Though this issue disappears entirely if the sub is switched to pass by reference in order to fix the other problems.



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


Comment on Re^2: Global symbol requires explicit package name
Download Code
Re^3: Global symbol requires explicit package name
by greengaroo (Hermit) on Nov 28, 2012 at 16:01 UTC

    Exactly! If you know you will pass a large structure to a subroutine, better pass it as a reference! Like this:

    # Calling routine routine ( $v1, $v2, \@array ); sub routine { my ( $fh, $h, $keys ) = @_; ... # Dereference the array in a loop: foreach my $key ( @{$keys} ) {

    Testing never proves the absence of faults, it only shows their presence.
      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

        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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-11-27 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (183 votes), past polls