Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
The stupid question is the question not asked
 
PerlMonks  

learning perl chapter 4

by singho (Novice)
on Jan 03, 2013 at 04:38 UTC ( #1011393=perlquestion: print w/ replies, xml ) Need Help??
singho has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get this work but i somehow get the below error, your help will be appreciated.
#!/usr/bin/perl use strict; use warnings; my ($fred, $barney); sub max { print "You are using the subroutine max"; if ( $fred gt $barney ) { $fred; } else { $barney; } } my $n = &max(10, 15); print "$n";

the error i get is

Use of uninitialized value in string gt at learningperl4.pl line 10.
Use of uninitialized value in string gt at learningperl4.pl line 10.
Use of uninitialized value in string at learningperl4.pl line 18.

Comment on learning perl chapter 4
Download Code
Re: learning perl chapter 4
by davido (Archbishop) on Jan 03, 2013 at 04:46 UTC

    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.


    Dave

      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.
Re: learning perl chapter 4
by AnomalousMonk (Monsignor) on Jan 03, 2013 at 06:36 UTC

    Another problem with the OPed code is that it is apparently trying to do numeric comparison with the  gt string comparison operator, which does lexicographic comparison and will silently stringize its operands. The problem with this is that "2" is lexicographically greater than "10". Solution: use  < <= == != >= > <=> numeric comparison operators for numbers. See Relational Operators and Equality Operators in perlop.

    >perl -wMstrict -le "sub max { my ($fred, $barney) = @_; ;; if ($fred gt $barney) { return $fred; } else { return $barney; } } ;; my $n = max(2, 10); print $n; " 2

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-04-19 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls