in reply to Re^3: Tie::File, is untie file handle must?
in thread Tie::File, is untie file handle must?

what makes it better to change that value from zero than from undefined?

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^5: Tie::File, is untie file handle must?
by u65 (Chaplain) on Aug 05, 2015 at 00:30 UTC

    Well, I can't remember what is supposed to happen with Perl, but it definitely isn't something a C/C++ coder would do. And it will look better when it's revisited years later. I haven't tried the idiom as the OP has it. Doesn't Perl squawk about the variable being undefined if using strict?

    Update:

    Thanks, 1nickt. Here is the explanation I was looking for (from p. 72 of Perl 6 Now: The Core Ideas Illustrated with Perl 5 by Scott Walters):

    Perl 5 throws you a Use of uninitialized value when you try to use a variable that contains undef, at least for most operations. use strict; use warnings; my $i; $i++; is acceptable; it was decided that trying to increment undef obviously means that undef should be interpreted as 0 and is common enough not to require explicitly declaring the variable. String operations, bitwise operations, and most math operations will throw this warning.

    So the idiom is okay and will not cause a squawk, but I still maintain that, in code to be shared and used by others, one should initialize the variable, and I think Damian Conway would agree it is a best practice (I'll see if I can find a reference to this situation in his book).

    Update 2:

    I couldn't find anything specific, so I can't fairly claim a Conway best practice, but I still claim explicitly initializing the counting variable adheres to Prof. Conway's goal of Maintainability where he says, among other things,

    ...Better yet, try to optimize for comprehensibility: easy-to-read and easy to understand aren't necesarily the same thing.

    I shall try not to complain about the iterated-undefined-index-variable idiom again, although I will continue to initialize it in my code as a personal best practice.

      When you declare the scalar variable like this:

      my $foo;

      . . . the value of $foo is undef. This is an actual value that is different from 0 and an empty string, and can be tested for.

      Perl will say nothing when you declare the variable and will assign undef to it.

      At that point you can test to see whether or not it has been given a defined value:

      if ( defined( $foo ) )

      Perl will then do the right thing when you use the variable, i.e. assigning it the value of zero if you use it as a number, or an empty string if you use it as a string. If you have warnings enabled, this is where Perl will squawk:

      #!/usr/bin/perl use strict; use warnings; my $foo; print $foo * 3;
      Use of uninitialized value $foo in multiplication (*) at -e line 1. 0

      Hope this helps

      Edit: added example of defined(), warnings output

      The way forward always starts with a minimal test.