Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: learning perl chapter 4

by davido (Archbishop)
on Jan 03, 2013 at 04:46 UTC ( #1011394=note: print w/replies, xml ) Need Help??

in reply to learning perl chapter 4

Move "my( $fred, $barney );" into the subroutine, and assign the parameter list to it, like this:

sub max { my( $fred, $barney ) = @_; print "You are using the subroutine max.\n"; #....etc...

The issue is that you're never unpacking any arguments inside of the sub, so $fred and $barney are never assigned a value. The other problem is that currently $fred and $barney are being declared at the wrong scope.

To avoid confusing behavior, your subroutine ought to also be returning a value explicitly, rather than relying on the behavior of returning the value of the last expression to be evaluated. It will work, but I never like seeing return values being created inside of an if/else block without explicitly using "return" as a visual cue of what's going on.


Replies are listed 'Best First'.
Re^2: learning perl chapter 4
by singho (Novice) on Jan 03, 2013 at 06:19 UTC
    thanks a lot it worked well, this is so stupid of me, since the error itself was so much clearly stating that i am using an uninitialized value. Thanks once more, i will try to be more careful.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2018-06-23 14:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.