Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

uninitialized value warning in reading file

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

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found