$_ is a global variable. Your while is setting it to the last record of your filehandle and since your files end with a \n, $_ returns blank (I guess $_ gets set before the test is done to see if it has a value). Since this is Perl there is more than one way to get around this:
You can localize $_ in your sub like so:
sub screwed {
my ($in) = @_;
local $_; # basically a temporary $_ just for this sub
open(IN, "<$in");
while(<IN>) { print "file $in contains $_"; }
close(IN);
}
Or you can set each line of the file to a variable in the while like so:
while(defined(my $foo = <IN>)) { print "file $in contains $foo"; }
Hope that helps
Lobster Aliens Are attacking the world!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|