in reply to nested <FILE> read returns undefined?
Because you've used a for loop, the first instance of <FILE> reads the entire file into memory (the place where you've put the read is in list context and perl slurps in list context). The second read occurs at the end of the file, so you get undef. If you really want to do this, use a while loop instead:
The conditional of a while is in scalar context, so only one line at a time will be read.while (my $line = <FILE>) { my $x = <FILE>; # ... }
It's interesting to note that perl6 will change this common idiom such that a for loop does the Right Thing. (for those interested, it would look something like this: for =$filehandle -> $line { my $x = =$file_handle; ... } or when you're going to do the same as while (<>) { ... } in perl5, it's for =<> { ... } in perl6.)
|
|---|