Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

check undef

by Sun751 (Beadle)
on Jun 23, 2009 at 07:19 UTC ( #773899=perlquestion: print w/replies, xml ) Need Help??
Sun751 has asked for the wisdom of the Perl Monks concerning the following question:

In below code i am trying to check if the variables are undef , could any one suggest me better way???
if ($WCkey eq undef && $SCkey eq undef && $Pkey eq undef && $ +Wkey eq undef && $Ukey eq undef) { print "bla bla bla"; }

Replies are listed 'Best First'.
Re: check undef
by moritz (Cardinal) on Jun 23, 2009 at 07:29 UTC
    Use the defined function to check if something is (not) undef.

    In perl-5.10 and later you can also use the smart-match operator:

    if ($WCkey ~~ undef && $SCeky ~~ undef && ... ) { }
      why dont you try something like below if (! defined $WCkey) cheers srikrishnan
        If I try that I get
        Semicolon seems to be missing at 2 line 4. syntax error at 2 line 5, next token ??? Execution of 2 aborted due to compilation errors.
        Just kidding :D code tags intended for code/data/error messages
Re: check undef
by Corion (Pope) on Jun 23, 2009 at 07:24 UTC

    When comparing a value against a constant, always write the constant to the left side like this:

    if (undef = $WCkey) { ...

    If you make the error you made in your original post, Perl will immediately tell you.

    Also, the function for checking whether a value is undef or not is the defined function.

    Update: The if (undef = $WCkey) { ... is not a typo but intended for demonstration. Perl raises a runtime error when you use

    if (undef = $WCkey) { ...

    instead of

    if (undef == $WCkey) { ...

    while it doesn't even raise a warning if you write the "comparison" the other way around:

    >perl -we "if (undef = $foo) { print 'bar' }" Name "main::foo" used only once: possible typo at -e line 1. Modification of a read-only value attempted at -e line 1. >perl -we "if ($foo = undef) { print 'bar' }" Name "main::foo" used only once: possible typo at -e line 1.

    So, in the spirit of building a defensive habit, write the constant to the left side of comparisons for equality.

      Sorry about my original post, Now the problem is, I know defined and how to use it but the problem here is, if those variable are "undef" do some thing else, if (defined($key)) { .... } but is there any thing like this if (undefined($key)) { .... } Any suggestion???

        You have not programmed much, have you? The perlop manpage does not hide how to invert logic expressions. See logical not for how to execute something if a condition is not true.

        For futher reading, I recommend looking at Truth Tables and potentially then Boolean Logic.

Re: check undef
by linuxer (Curate) on Jun 23, 2009 at 09:33 UTC

    If you have to do these checks again and again, you can think of creating a dedicated sub routine for that.

    sub all_defined { for my $i ( @_ ) { #return 0 if !defined $i; return if !defined $i; } return 1; } if ( all_defined( $WCkey, $SCkey, $Pkey, $Wkey, $Ukey ) ) { print "bla bla"; }

    Update: modified code; thanks davorg

      return 0 has a potentially dangerous effect. It will evaluate as true if your subroutine is called in list context. Just a bare return is usually a better idea.


      See the Copyright notice on my home node.

      Perl training courses

        Not better, different. Now it breaks under different circumstances.
Re: check undef
by Anonymous Monk on Jun 23, 2009 at 07:23 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://773899]
Approved by targetsmart
[1nickt]: Morning Marto!
marto waves

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2017-12-17 13:21 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (464 votes). Check out past polls.