Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Undefined vs empty string (not length)

by Anonymous Monk
on Jun 05, 2013 at 03:08 UTC ( [id://1037098]=note: print w/replies, xml ) Need Help??


in reply to Undefined vs empty string


if( not length $var ){ ...
if( @array ) { ...
if( %hash ) { ...
See also [id://980771(on truthiness)]
  • Comment on Re: Undefined vs empty string (not length)

Replies are listed 'Best First'.
Re^2: Undefined vs empty string (not length)
by BrowserUk (Patriarch) on Jun 05, 2013 at 03:18 UTC
    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
        In perl5120delta:
        C<length undef> now returns undef.

        A subtle and useful change that had slipped under my radar.


        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.

        5.12? *sigh* I've adopted that idiom too quickly, then. We are still at 5.10 at work (and 5.10 was brand new at the point we started the process of upgrading to a new Perl so it may well be a while before we finish the next jump in version numbers even if we started that right now -- though I expect the next attempt to happen significantly faster given the lack of Solaris and not also doing an upgrade from Apache 1 to Apache 2).

        Though, I find that in a large majority of cases, '0' is not a meaningful value and so "! $s" and "$s || ..." are just fine. But it is annoying that the only moderately infrequent case of "undef and empty string mean 'unset' but '0' does not" requires testing the variable twice in order to deal with it (without risk of warnings).

        Some readers might like to know that using "not length $s" can easily bite one in quite mundane situations. I'd only use "! length $s".

        - tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found