I wrote some code (about 100 lines) that is working fine on version 5.12.1. Unfortunately my client is using version 5.10.0. So I tested the code on 5.10.0 and found it doesn't work!
Where can I find a list of differences between 5.10 and 5.12?
I think the best answer to the question of "Where can I find a list of differences between 5.10 and 5.12" is plusplus' comment under the "accepted answer". For the explanation of the code below, please read Michael Carman's answer.
The code that works on 5.12.1 but does not work on 5.10.0 ($contents
is still an empty string after running the code)
# read in the first 10 lines.
my $contents = '';
for (my $i = 0; $i < 10 && ! eof; $i++) {
$contents .= <FILE>;
}
The improved code that works on both versions.
# read in the first 10 lines.
my $contents = '';
my $i = 0;
while (<FILE>) {
last if $i >= 10;
$contents .= $_;
$i++;
}
There's a bug in your first code sample. A bare eof
reports status for the last filehandle read. On of the first pass through the loop you (presumably) haven't read anything yet; not anything from FILE
anyway. It appears that the internal behavior of this invalid call changed. Under Perl 5.12.1 running perl -E "say eof"
prints nothing. Under Perl 5.10.0 it prints "1".
Explicitly testing eof(FILE)
should fix the problem.
Tangent: Your code isn't very idiomatic. A more perlish approach would be
my $content;
while(<$fh>) {
if ( 1 .. 10 ) { $content .= $_ }
else { last }
}
The idioms used are:
$fh
instead of FILE
)..
to track the number of lines read. This form
implicitly tests against the input line number $.
.last
to break out of the loop early.Look at the perldoc page. You''ll find the perldelta's there. Or post your code and have us look at it ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With