Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Uninitialized value

by ravendarke (Beadle)
on May 10, 2002 at 16:29 UTC ( [id://165699]=perlquestion: print w/replies, xml ) Need Help??

ravendarke has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks, I ran into a problem where I needed a function similar to Oracle's NVL function. For those of you who aren't Oracle people, the function substitutes a value for a returned NULL. A co-worker pointed me to the ( ? : ) operators. However, I've found a small problem with it. Well, let me show you.
my $junk; print 1+$junk;
Returns an unitialized value warning, as expected.
print 1+($junk ? $junk : 0);
Returns 1, no warning.
$junk = 11; print 1+($junk ? $junk : 0);
Returns 12, seems to do what I want, but...
$junk=0; print 1+($junk ? $junk : 15);
returns 16, even though the value was defined as 0.
So, clearly this only worked because I didn't need a different number to be returned. I can see this coming to bite me later. So, I guess my question is, does anyone know of a function that will do what I'm looking for? Is this an obvious misuse of ? : operators?

I'm sorry this was so wordy for so small a question..
Marty

Replies are listed 'Best First'.
Re: Uninitialized value
by ariels (Curate) on May 10, 2002 at 16:32 UTC

    You want to know if $junk is defined, why not check if defined($junk)? Handles <samp>0</samp> and <samp>''</samp> correctly, is easily readable, and explains itself.

Re: Uninitialized value
by boo_radley (Parson) on May 10, 2002 at 16:39 UTC
    ravendarke says :
    returns 16, even though the value was defined as 0.


    You've almost stumbled onto an answer by yourself, ravendarke.
    use warnings; my $junk; print 1+$junk; print "\n"; #Returns an unitialized value warning, as expected. print 1+(defined($junk) ? $junk : 0); print "\n"; #Returns 1, no warning. $junk = 11; print 1+(defined($junk) ? $junk : 0); print "\n"; #Returns 12, seems to do what I want, but... $junk=0; print 1+(defined($junk) ? $junk : 15); print "\n"; # now prints out 1.
    The secret sauce is in the definedness of your variable.
Re: Uninitialized value
by chromatic (Archbishop) on May 10, 2002 at 16:32 UTC
Re: Uninitialized value
by zzspectrez (Hermit) on May 10, 2002 at 16:41 UTC

    > my $junk; > print 1+$junk;
    > Returns an unitialized value warning, as expected.
    > print 1+($junk ? $junk : 0);
    > Returns 1, no warning.

    This should not return a warning. This statement says, if $junk is true then return $junk add 1 to it and print the result. If $junk is false, which is the case since it is undefined, then return 0 and add 1 to it and print the result.
    I think what you meant is if you wrote it like:

    print 1+ ( $junk ? 0 : $junk);
    Then it will return a warning if $junk is undefined.

    zzspectrez

Re: Uninitialized value
by mephit (Scribe) on May 10, 2002 at 17:09 UTC
     print 1 + ($junk && ($junk != 0) ? $junk : 0); Does that do what you want?
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-26 00:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found