4

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++;
} 
Community
  • 1
  • 1
powerboy
  • 10,523
  • 20
  • 63
  • 93
  • 6
    Obviously your Heisenberg compensator is out of alignment and needs to be repolarized. Or that's my best guess without seeing an error message. – Schwern Jul 13 '10 at 18:35
  • Tied filehandles now have an additional method EOF which provides the EOF type. – Konerak Jul 13 '10 at 19:49
  • "Doesn't work" isn't a useful description. You need to tell us 1) what you expected it to do, and 2) what it actually did. – Michael Carman Jul 13 '10 at 20:18
  • @Michael - I though the comment in the code is clear enough to tell you that I expected to read 10 lines from a file. "doesn't work" means that the code fails to do that on version 5.10. Anyway, I've edited the question to make my point more clear. Thx. – powerboy Jul 13 '10 at 20:37

2 Answers2

9

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
  • 30,628
  • 10
  • 74
  • 122
  • ahh..seems that this is the problem of my code snippet. I though `eof` worked in the same way as that in C – powerboy Jul 13 '10 at 20:46
  • Good points in the "tangent"! Thanks! I am still thinking in a C/Java way. Need to be more perlish ;) – powerboy Jul 13 '10 at 20:53
4

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

Konerak
  • 39,272
  • 12
  • 98
  • 118
  • 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 Jul 13 '10 at 08:08
  • 10
    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 Jul 13 '10 at 08:17
  • I've posted my code in the question. Anyone know why the for loop is not working on version 5.10? – powerboy Jul 13 '10 at 19:08