Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Undefined vs empty string

by Anonymous Monk
on Jun 05, 2013 at 02:00 UTC ( [id://1037087]=perlquestion: print w/replies, xml ) Need Help??

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

I always assumed undefined and empty string are treated differently and did test for undefined and empty strings the following way.
if (!defined $string || $string eq "") { }

I feel this is perhaps not right. If so, how this can be coded appropriately.

Replies are listed 'Best First'.
Re: Undefined vs empty string
by BrowserUk (Patriarch) on Jun 05, 2013 at 02:30 UTC

    Depends somewhat on what you are going to do in the body og the if, but defined and length will generally ensure that a variable is defined and contains something; so negate that:

    say "'$_'", ' ', !(defined and length) ? 'go' : 'nogo' for undef, '', +0, 'fred';; Use of uninitialized value $_ in concatenation (.) or string at (eval +16) line 1, <STDIN> line 8. '' go '' go '0' nogo 'fred' nogo

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      So if my intention is to ensure that a variable is defined and contains something, should I change my code to how you have recommended or what I have is OK?

        Much of a muchness. length is a tiny bit quicker than eq, but otherwise I think they will act similarly.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Undefined vs empty string
by Athanasius (Archbishop) on Jun 05, 2013 at 02:29 UTC
Re: Undefined vs empty string
by Cristoforo (Curate) on Jun 05, 2013 at 02:34 UTC
    I think you could just write if (!$string) if you were sure $string couldn't be 0 or '0'. (a zero or zero in quotes)

    Not sure if that answers your question.

    Update: Just saw that Athanasius and BrowserUk posted similar answers.

Re: Undefined vs empty string (not length)
by Anonymous Monk on Jun 05, 2013 at 03:08 UTC

    if( not length $var ){ ...
    if( @array ) { ...
    if( %hash ) { ...
    See also [id://980771(on truthiness)]
      if( not length $var ){ ...
      Use of uninitialized value $_ in length

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Use of uninitialized value $_ in length

        What perl version? Something before 5.012002 right? something like 5.008008?

        perl -Wle " print $] if not length $_ " 5.012002
Re: Undefined vs empty string
by Anonymous Monk on Jun 05, 2013 at 03:36 UTC
    Thank you, everyone.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-03-19 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found