Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: The error says the value is uninitialized, but it works anyway

by haukex (Archbishop)
on Aug 18, 2019 at 07:42 UTC ( [id://11104642]=note: print w/replies, xml ) Need Help??


in reply to Re: The error says the value is uninitialized, but it works anyway
in thread The error says the value is uninitialized, but it works anyway

undef ne $string if $string is a defined value.

Sorry, but no: $string = "" is a defined value, and undef eq "". What's happening is that eq forces string context on its arguments and undef is converted to "", with a warning, which of course is the same thing as the string "". eq is not a test for definedness, only defined does that - undef eq undef not because they are both undef, but because "" eq "".

Replies are listed 'Best First'.
Re^3: The error says the value is uninitialized, but it works anyway
by jcb (Parson) on Aug 18, 2019 at 22:30 UTC

    Hmmm... You are right. I have edited my earlier post to correct the misinformation.

    It seems that I had confused Perl, SQL, and Lisp while writing that. As I recall, there is only one undef value in perl, and DBI (see also DBI recipes) maps SQL NULL and Perl undef. Here are the tests I ran before writing that post:

    • $ perl -we 'print "T\n" if "foo" eq "foo"'
      T
    • $ perl -we 'print "T\n" if undef eq "foo"'
      Use of uninitialized value in string eq at -e line 1.
    • $ perl -we 'print "T\n" if undef eq undef'
      Use of uninitialized value in string eq at -e line 1.
      Use of uninitialized value in string eq at -e line 1.
      T

    Spotted the missing test yet? Try:

    • $ perl -we 'print "T\n" if undef eq ""'
      Use of uninitialized value in string eq at -e line 1.
      T

    Since the message specifically mentions use of undef in eq, I had assumed that eq actually sees the undefined value, instead of stringifying it like any other value. Wrong!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found