http://qs321.pair.com?node_id=272916

dannoura has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

This should be a simple one. Whenever I use code similar to:

#! c:\perl\bin use strict; use warnings; open (FH, "somefile.txt") or die("cannot open file"); foreach (<FH>) { print if ($_=~/foo/); }

I get a lot of uninitialized value warnings. I assume this is because there are empty lines at the end of the file, but I'm not sure and also even if this is the case I don't know how to get rid of the warnings (except for turning off use warnings).

Any ideas?

Replies are listed 'Best First'.
Re: uninitialized value warning in reading file
by ant9000 (Monk) on Jul 10, 2003 at 10:12 UTC
    Use this construct for looping
    while(defined($_=<FH>)){
    and you should be fine.
      Hi,

      as i understood it up to now there is no difference between (defined($_=<FH>)) and (<FH>).

      Have a look at man perlop, Page 33.

      Perhaps it could be useful if dannoura shows us an excerpt of his somefile.txt.

      greetings, tos

Re: uninitialized value warning in reading file
by Anonymous Monk on Jul 10, 2003 at 10:21 UTC

    Not an alternative to better coding practice suggested by ant9000 but:

    #!/usr/bin/perl use strict; use warnings; no warnings qw(uninitialized); #turn off uninit warnings
    See perldoc perllexwarn ...