views:

144

answers:

2

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?


Edit:

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++;
} 
+4  A: 

Look at the perldoc page. You''ll find the perldelta's there. Or post your code and have us look at it ;)

Konerak
The code is about 100 lines. I'll try to find out those lines that cause the issue and post it here tomorrow. Thanks in advance! Good night!
powerboy
because it's not immediately obvious from the titles, http://perldoc.perl.org/perl5120delta.html is the differences between the major version numbers 5.10 and 5.12.0 and http://perldoc.perl.org/perl5121delta.html is the differences between 5.12.0 and 5.12.1 - you'll need to check both, although the first will probably be most useful
plusplus
I've posted my code in the question. Anyone know why the for loop is not working on version 5.10?
powerboy
+7  A: 

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:

  • Use a lexical filehandle instead of a typeglob. ($fh instead of FILE)
  • Use the range operator .. to track the number of lines read. This form implicitly tests against the input line number $..
  • Don't explicitly test for EOF (let the loop handle it).
  • Use last to break out of the loop early.
Michael Carman
ahh..seems that this is the problem of my code snippet. I though `eof` worked in the same way as that in C
powerboy
Good points in the "tangent"! Thanks! I am still thinking in a C/Java way. Need to be more perlish ;)
powerboy