Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

what is *undef* ?

by kiseok7 (Beadle)
on May 25, 2001 at 03:50 UTC ( #83174=perlquestion: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: what is *undef* ?
by japhy (Canon) on May 25, 2001 at 03:52 UTC
    The only way to test if something is undef is to see if it's not defined:
    if (not defined $x) { print "\$x is undef\n"; }
    Using undef in numeric context (like you did) makes it look like 0; using it as a string makes it look like "".

    japhy -- Perl and Regex Hacker
Re: what is *undef* ?
by tachyon (Chancellor) on May 25, 2001 at 06:15 UTC

    You seem unclear between the difference between defined and false. This code should show you the difference.

    In perl falseness includes these three miscreants only: 0 "" and undef. All else is true

    UPDATE: Chipmunk did not like my evals so I have used 5 different perl idioms for if (condition){print this}else{print that}. Choices, choices. TIMTOWTDI TIMTOWODI. To avoid confusion these idioms all do exactly the same thing - test a condition and print someting if it is true and something different if it is not.

    tachyon

    my $a; print "my \$a;\n"; print "Undefined\n" unless defined $a; print "False\n" unless $a; $a=0; print "\nmy \$a=0;\n"; if (defined $a) { print "Defined\n"; } else { print "Undefined\n"; } if ($a) { print "True\n"; } else { print "False\n"; } $a=''; print "\nmy \$a='';\n"; print eval'(defined $a) ? "Defined\n" : "Undefined\n"'; print eval'($a) ? "True\n" : "False\n"'; $a=1; print "\nmy \$a=1;\n"; print ((defined $a) ? "Defined\n" : "Undefined\n"); print (($a) ? "True\n" : "False\n"); $a='foo';print "\nmy \$a='foo';\n"; print '',(defined $a) ? "Defined\n" : "Undefined\n"; print '',($a) ? "True\n" : "False\n"; undef $a; print "\nundef \$a;\n"; (defined $a) ? print "Defined\n" : print "Undefined\n"; ($a) ? print "True\n" : print "False\n";

    This is about the only printing idiom that does not work! Sadly it is also the most elegant to my eyes.

    print (defined $a) ? "Defined\n" : "Undefined\n";

    This only works with a null string and comma after the print as shown above print '', (cond)? foo : bar

      actually, noyhcat, the problem with your elegant version is that it makes print act like a function (darn those parentheses, eh beatnik?). there are two easy solutions you don't mention:

      print ((defined $a) ? "Defined\n" : "Undefined\n");
      or
      print +(defined $a) ? "Defined\n" : "Undefined\n";
      the first uses another set of parens to pass print the right stuff. the second uses perl's mystifying unary plus. unlike some languages, where unary plus does something useless, like take the absolute value of the argument, perl's unary + serves the important function of being a non-parenthesis. this means that things like warn, print, and my_user_func won't be handed the contents of that set of parens as their sole argument list.

      isn't that something? unary + is nothing. there's nothing like it; all it does is *be* something.

      .

      update: mmm, yes, japhy smart. vynce not so smart. obvious answer is, like japhy says, to move the parens to just around defined's argument. but i'm still happy to have gotten to explain the useful use of perl's unary +.

      .
        Or use the parentheses around the argument to defined():
        print defined($a) ? "Defined\n" : "Undefined\n";
        That looks more sensical than some floating + to make things work.

        japhy -- Perl and Regex Hacker
Re: what is *undef* ?
by dvergin (Monsignor) on May 25, 2001 at 09:16 UTC
    Just for fun, let's come at this from another direction. Your test would more accurately read:
    print "undef evaluates as zero in a numerical context" if (undef == 0);
    It was not $a (equaling zero) which was coerced to equal undef. Just the opposite. It was the undef that, in a numerical context (occasioned by the '==' test), was coerced into being taken as zero.

    Further evidence of what Perl is doing can be obtained by running with -w in your hash-bang line. Perl will complain: Use of uninitialized value in numeric eq (==) at myprog.pl line 4. and then do its best to render an acceptable result by treating the undef as zero and concluding that the test succeeds.

    HTH

Re: what is *undef* ?
by Vynce (Friar) on May 25, 2001 at 15:50 UTC

    <JOKE>"*undef*" is a more powerful version of the spell "undef"</JOKE>

    more seriously, undef is a builtin function that removes the definition from a variable, and returns a special value also known, perhaps unfortunately, as undef. as a value, it represents something which hasn't been defined. since things which aren't defined are hard to add or print, perl has to assume something about what to do in those situations. what it does is pretend zero (for addition or other number-based things) or an empty string (for printing or other string contexts). you'll note that adding zero and printing empty strings are pretty safe, sane things to do.

    admittedly, multiplying by zero can be bad, as can regex-matching empty strings, but hey. you shouldn't use undefined values in those situations anyway.

    .

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2023-03-24 10:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (61 votes). Check out past polls.

    Notices?