Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: how do subroutines work

by nefigah (Monk)
on Mar 22, 2008 at 20:34 UTC ( #675675=note: print w/ replies, xml ) Need Help??


in reply to how do subroutines work

You should note that your subroutine here didn't "load" any part of the %list hash into @_ at all; instead you are relying on the fact that somewhere out there is a hash called %list that works how you want it to. As you can imagine, this is not necessarily the safest way to code. You would be better off either making the hash in the subroutine itself (if it's the only thing that uses it) or passing the hash as a parameter (or an element of the hash as a parameter, depending on what is needed) to the sub. This way everything doesn't break if things get moved around or messed with.

Also, as Pancho pointed out, names are important, especially when you're a beginner to help understand what's going on. I imagine the line:

my $list = $list{$animal}

is quite confusing, because you are making a new scalar variable that has the same name ('list') as a hash that you are accessing an element of (meaning you have to use the $ in front of both). This is legal but confusing. Also confusing is that you are calling the scalar $list, when in fact it's not a list at all.

I'm not sure if this is more advice than you bargained for, but do keep it in mind; it will make what's going on more obvious!


I'm a peripheral visionary... I can see into the future, but just way off to the side.


Comment on Re: how do subroutines work
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-09-01 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (299 votes), past polls