Loading ...
Sorry, an error occurred while loading the content.
 

Re: NULL vs zero From: merlyn@stonehenge.com

Expand Messages
  • Andy Bach
    ... Perl treats zero (the number) and the empty string as false. However, a variable w/ a value of zero is defined, so to separate them:for my $val ( (qw/1 a
    Message 1 of 1 , Jan 30, 2008
      > How do your smart guys write codes to tell 0 from NULL?

      Perl treats zero (the number) and the empty string as false. However, a
      variable w/ a value of zero is defined, so to separate them:for my $val
      ( (qw/1 a 0/, undef ) ) {
      if ( $val ) {
      print "Val is something and non-zero: $val\n";
      }
      elsif ( defined($val) ) {
      print "Val is zero?: $val\n";
      }
      else {
      print "Val must be undef: $val (get a warning?)\n";
      }

      } # for


      Val is something and non-zero: 1
      Val is something and non-zero: a
      Val is zero?: 0
      Use of uninitialized value in concatenation (.) or string at -e line 11.
      Val must be undef: (get a warning?)

      Another route is to check the length of the var e.g. "if ( length($var)
      > 0 ) { ..." A str of all blanks has a length though it will be false
      and be (coherced) into being '==' to zero.

      a
    Your message has been successfully submitted and would be delivered to recipients shortly.