in reply to Re: Re: Re: advice for reading data from a file
in thread advice for reading data from a file

thanks for the explanation Roger,

but concerning the 'defined' in the loop my code break breaks with this error:

Use of uninitialized value in scalar chomp at /data/tmp/test_check_fie +ld.pl line 31, <$fh> line 2. <br><br> Use of uninitialized value in split at /data/tmp/test_check_field.pl l +ine 32, <$fh> line 2. <br> *** ERROR number of fields !=4
after reading perldoc 'defined' :

defined Returns a Boolean value telling whether EXPR has a value other than the undefined value "undef". If EXPR is not present, $_ will be checked.

it seems to me that it's normal that my code break as it's not the value of the line that is returned but instead the boolean value of 'defined'..or am i wrong somewhere ?

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: advice for reading data from a file
by Roger (Parson) on Jan 19, 2004 at 01:30 UTC
    My mistake, should be while (defined ($_ = <$fh>)) {.... Forgot the bracket arround <$fh>. graff suggests that the defined check is implicit, but I would still leave the code as it is. A bit of defensive programming would not hurt.

      Actually, it's not just me. The perlop man page (way down in the section on "I/O Operators") says:
      The following lines are equivalent: while (defined($_ = <STDIN>)) { print; } while ($_ = <STDIN>) { print; } while (<STDIN>) { print; } for (;<STDIN>;) { print; } print while defined($_ = <STDIN>); print while ($_ = <STDIN>); print while <STDIN>;
        Yes you are quite right. I tried the following script and I am convienced. I have mixed it up with looping through array elements the other day, where I was caught by the '0' gotcha' and became a bit paranoid.
        use strict; use warnings; while (<DATA>) { print; } __DATA__ 0 1 2 3