Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

uninitialized value warning in reading file

by dannoura (Pilgrim)
on Jul 10, 2003 at 08:40 UTC ( #272916=perlquestion: print w/replies, xml ) Need Help??

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


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
    and you should be fine.

      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 ...

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://272916]
Approved by valdez
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2021-12-03 13:28 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (29 votes). Check out past polls.