Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: while-loop and reading over EOF?



Hmm. I suspect you've got Perl versionitis going on. I get the same results on OS X and on Linux. But the odd part is that I get the same *wrong* results. I've got is v5.6.0 on both boxes.

Here's my somewhat simplified test program:
-------------------------
#!/usr/bin/perl
undef $/;
open(H, '<test') or die "open: $!\n";
while(<H>) {
print "here\n";
}
-------------------------
This prints "here" twice.

But this program should be logically identical, but it produces the expected results.
-------------------------
#!/usr/bin/perl
undef $/;
open(H, '<test') or die "open: $!\n";
if(<H>) { print "here\n"; }
if(<H>) { print "here\n"; }
-------------------------
This prints "here" only once.

Looks like a Perl bug to me that exists at least in v5.6.0. The latest stable version is 5.6.1 - I'm getting that now.

Chuck


At 6:48 PM +0100 3/9/02, Sebastian Hoffmann wrote:
I hope this is the correct list to ask my question - otherwise, please ignore.

I wonder whether anyone could explain the following OS X specific odd behaviour to me. I have the following simple perl script test.pl:

-----------------------------------------
#!/usr/bin/perl

undef $/;

$path = '/path/to/some/file.txt';

open(H, "<$path") || die("Couldn't open file: $path");
while (<H>) {
warn "got here";
$file=$_;
$length = length($file);
warn $length;
}
close(H);

exit(0);
-----------------------------------------

Under linux, this works fine - "undef $/" causes the whole file to be sucked in at once (because no record separator is defined) and $file contains what it would be expected to. I'm aware that it is of course in this case nonsense to put this into a while-loop - but it has brought my attention to the following behaviour under OS X - here's the output I get when running the above script:

got here at test.pl line 9, <H> chunk 1.
20508 at test.pl line 12, <H> chunk 1.
got here at test.pl line 9, <H> chunk 2.
0 at test.pl line 12, <H> chunk 2.

In other words - the while-loop is executed twice. It's as if EOF was different under OS X from Linux but perl doesn't know about it (or something like that?)....

If I change the second line to

$/ = \x04; # (or something else I know that doesn't exist in the file...)

then the while-loop is only executed once.

Any hints, suggestions, etc?
Thanks in advance!
Sebastian
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.

References: 
 >while-loop and reading over EOF? (From: Sebastian Hoffmann <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.