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

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://675675]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2018-02-23 07:19 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (300 votes). Check out past polls.