Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Global symbol requires explicit package name

by greengaroo (Hermit)
on Nov 28, 2012 at 15:34 UTC ( [id://1006051]=note: print w/replies, xml ) Need Help??


in reply to Global symbol requires explicit package name

Sorry for jumping in, but I have to say something! To me it's as if someone scratches his nails on a chalkboard!

This:

my $fh=$_[0]; my $h=$_[1]; shift @_; shift @_; foreach my $key (@_)
... is not clean!

Try this:

my ( $fh, $h, @keys ) = @_; foreach my $key ( @keys ) {
Or this if you prefer:
my $fh = shift; my $h = shift; foreach my $key ( @_ ) {

That's my 2 cents.

Testing never proves the absence of faults, it only shows their presence.

Replies are listed 'Best First'.
Re^2: Global symbol requires explicit package name
by ColonelPanic (Friar) on Nov 28, 2012 at 15:42 UTC

    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

      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
Re^2: Global symbol requires explicit package name
by cunningrat (Acolyte) on Nov 28, 2012 at 17:30 UTC

    Thank you all for the replies. Even though I figured it out myself, your comments were VERY helpful as far as helping me write cleaner, better code goes.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2025-04-25 19:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.