Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: Do people find warning for undef with string compare useful?

by vsespb (Chaplain)
on Jun 01, 2013 at 11:06 UTC ( [id://1036415]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Do people find warning for undef with string compare useful?
in thread Do people find warning for undef with string compare useful?

I don't see how the eq case is any more helpful than the logical-only operators..?
Perhaps we should ask then why logical-only operators ignore undef, but not why other operators don't ignore it.
I just think of the amount of code out there that is written to avoid those warnings.
Really?
a code to avoid warning and introduce bug:
$x = '' unless defined($x)
a code to just avoid warning;
my $x = '';
a code to avoid bug:
defined($x) or confess;
So you see 1st case in your code?

a code with bug:
mysub($user_data) { $user_$data->{invoice_number} ... } mysub({age => $user_age, inovice_number = $invoice_number}) # TYPO!
a code which catches bug:
mysub($user_data) { defined($user_$data->{invoice_number}) or confess; $user_$data->{invoice_number} ... } mysub({age => $user_age, inovice_number = $invoice_number}) # TYPO!

Replies are listed 'Best First'.
Re^4: Do people find warning for undef with string compare useful?
by perl-diddler (Chaplain) on Jun 01, 2013 at 15:18 UTC
    I think you are going off in left field. You asked:
    Perhaps we should ask then why logical-only operators ignore undef, bu +t not why other operators don't ignore it. I just think of the amount of code out there that is written to av +oid those warnings.
    I think I made this clear earlier. It seems you are trying to change the nature of the discussion or are grasping at straws to prove some unknown point.

    You wouldn't turn off warnings for things that compare numbers or magnitude, as it is, inherently an error if you test 'undef' with an operator that compares magnitude. That TRULY, is an indication that you were expecting something that would have relative magnitude to something else, but instead, have something that is completely undefined.

    They are completely different. Comparing if two variables have the same value or are both uninitialized, isn't unreasonable. Since 'undef' is often returned for false and a non-zero length string that doesn't eval to 0 is returned for true, someone might simply want to compare the results of two tests to verify that both returned true or both returned false. Sure there are 100's of other ways to do it -- it was just a bit of a poll to see what people used it for and how it was more useful giving warnings as it does now, vs. acting like and/or/not/xor operators.

    You really didn't need to go into reasons why you should use the 'fields' and 'parents' pragma's... they have nothing to do with eq/ne as near as I can tell. I'm not sure what you are getting at in your telling me what is a bug and what is not -- or at least now how that is pertinent to the discussion.

    If you can't come up with any good reasons why it should be the way it is, that's fine.

    BTW -- IMO, "catching an error", and handling it -- ideally would involve invoking an alternate method to do what you were trying to do -- that's what humans do, unless they give up. Programming computers to continue in spite of errors, is really what is needed in today's SW, since all of it has errors.

    The next step in programming, is getting to programs that continue to run and do the right thing in the face of unexpected input -- or at least not to roll over and die.

    I have programs that collect status information, and if something is wrong, they take steps to fix it -- progressively, as well as send email at increasing intervals -- up to a day apart as long as the problem continues. More and more, I'm trying to build in redundancy and recovery into my programs, so that they won't harass me for little stuff.

    IMO, I tend to believe that the warnings produced for eq and ne aren't worth the problems they turn up, but that may be because I check for errors in others ways (if not in the first pass, eventually, because eventually, I am almost guaranteed to hit every trouble spot!) ;-)

      "catching an error", and handling it -- ideally would involve invoking an alternate method to do what you were trying to do
      have programs that collect status information, and if something is wrong, they take steps to fix it -- progressively, as well as send email at increasing intervals
      Well, In my example you see 1) a typo 2) "confess" method. This means I catch programmer's error (i.e. it's an assertion).
      you are trying to change the nature of the discussion
      Okay, show me your real code where you need work with undefs..
        Where I ***need*** work with undefs? I call strawman!!! ;-)

        Though, seriously, just earlier today...

        sub same_subnet ($) { my $p = shift; my $net = $_[0]; RefCheck($net, __PACKAGE__); TPe "ptype=%s, netptype=%s", $p->{ptype}, $net->{ptype} unless $p->{ptype} && $net->{ptype}; return undef if $p->{ptype} ne $net->{ptype}; <<<----
        The line with the arrow next to it threw out warnings because it got passed down the wrong stuff. But it didn't help me find the problem sooner. First I added the TestPrintErr statement to see which one was undef (Perl didn't tell me). Then I wondered if I should put in a check for undef there, .... had one there, until I realized it can't happen unless something was broken upstream.

        All it needed to do was tell me that it didn't match -- it didn't need to die w/a warning. I would rather it have returned the undef, so the upper level routines would have pointed at the place in the code that was generating the undefs that got passed down to this level. Still don't know if I need the overhead of the refcheck statement (it dies on wrong typeref).

        That's about the best I can come up with on the spur of the moment, but 'need'... come on now!!!... Mighten the above pass for a reasonable place where it's more of a pain than not -- especially since the upper level routine that calls this one was the one that had the error and the one that got back all the 'undef's from interfaces that should have matched.

        Does it pass for an example?

Re^4: Do people find warning for undef with string compare useful?
by perl-diddler (Chaplain) on Feb 11, 2014 at 21:47 UTC
    Looking through old stuff... I saw
    mysub($user_data) { $user_$data->{invoice_number} ... } mysub({age => $user_age, inovice_number = $invoice_number}) # TYPO!
    For most people the above would be caught by warnings+strict ... using an undeclared var, and/or only 1 use of the var. So never happens. Vs. here you say:
    a code to avoid bug:
    defined($x) or confess;
    --- A program that dies unexpected has a bug. You didn't avoid a bug, but caught it and displayed it to the user.

    I guess in my programs, I rely on "undef" as a valid and useful value to mean something that isn't initialized yet and/or has not been assigned a value yet..

    Example -- in a web-reading program, I had a value $p->{content}, that kept coming back "undef" after I got the content back from an html library. The call returned '0', to indicate it had succeeded in querying the server, but somewhere, it set some error value to '404' (that I couldn't easily find in the documentation) -- but it was then I realized, that 'content' was undef not due to any bug -- but because that's is what was returned from the website! Undefined/uninitialized content. Once I figured that out, I could handle it as some sort of read error and retry or ignore the call later. I think I eventually found the place for the error to be returned -- in an 'async-handler' call to an error routine. Not what I was expecting.

    In the above -- if I was checking if a mirror of the site was the same as another, comparing 'undef' against 'undef' would be a desirable operation -- it means both didn't return an object for 'something' (providing it is only a few objects). If it returns randomly or too often, something else may be wrong.

    (unlikely anyone reads these old replies -- too bad there's not a notification to people when someone has replied to one of their messages, ..vs. the current method of looking at all your posts and going through them 1-by-1 and looking to see if there are new responses... ;^/...)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2024-03-28 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found