in reply to Re: Looking for less code
in thread Looking for less code

A simple rationale is that if you want to print $flag for debugging, "undef" is an error

How come? Last I looked it was a warning, and only if warnings are enabled.

Additionally it's a good idea to use Data::Dumper or something similar for debugging output anyway, so I don't see the point you're trying to make.

Another rationale would be that you KNOW that it is "false" not "undefined"!. So say so!

undef is also false - why would 0 be clearer false than undef? 0 is a number to me, not primarily a "false" boolean value.

Replies are listed 'Best First'.
Re^3: Looking for less code
by Marshall (Canon) on Aug 11, 2009 at 09:15 UTC
    Point 1: You should run with both "warnings" and "strict" enabled.

    Point 2: I recommend taking some 'C' classes and you will understand the difference between "undef" and "zero".

    Update: My comment about 'C' was not meant in a derogatory way. I stand by my assertion that code should run with both warnings and strict. Some performance increase can be obtained without warnings in well debugged code. But that was not the point here. There is a difference in general between, I know for sure that this $var is "false": ('',"",0) and I "don't know what the value is", (undef), so I am gonna assume "false".

    Update: minor typo punctuation corrections.

    #!/usr/bin/perl -w use strict; my $x = undef; print "$x\n"; $x = 0; print "$x\n"; #prints: #Use of uninitialized value $x in string at C:\TEMP\perl1.pl line 5. 0
      Point 1: You should run with both "warnings" and "strict" enabled.
      I know. Still it's not an error, and still you should also use Data::Dumper (or better) for debugging (and then you don't even get a warning). But didn't I say that already?
      Point 2: I recommend taking some 'C' classes and you will understand the difference between "undef" and "zero".

      How do C classes help me here? To understand that an undef doesn't create an IV?

      I also know the difference between 0 and undef in perl. How does that relate to my second point? Both evaluate to false in boolean context, without warning.

        We are getting away from OP's question.

        I apologize for evidently starting that diversion. That was not my intent.

        Update:

        The OP wants to simplify some series of "if" statements. My personal opinion about a "false" value is that it should be a "defined value". BUT, an undef can be used in the false sense in an "if" statement!! No problem!! This will work!!

        If folks got the idea that I meant that "undef" did not equate to false, I certainly didn't mean that. As that is wrong! Undef will evaluate to "false".

        I personally think that when you assign a "false" value to a $var that "false" value should be "defined". Valid false values are single quotes '', double quotes "" and 0 and YES, undef. Using a "defined value" has advantages like when you print that $var, you don't get a run time error "undefined $var".

      I recommend taking some 'C' classes and you will understand the difference between "undef" and "zero".
      Unlike C, in Perl, "undef" is actually a value. I really fail what insight C can give you here. In fact, I recommend you take some Perl classes, and learn what undef really is (and isn't).
        Powerful poetry
        undef JavaFan
        Can't modify constant item in undef operator

        :)