Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > strange Perl behaviour

strange Perl behaviour
Thread Tools
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Zurich, Switzerland
Status: Offline
Reply With Quote
Apr 30, 2001, 04:55 AM
 
Dear all,
I'm rather puzzled by the following problem with Perl:

#!/usr/bin/perl

# suck in whole file
undef $/;

$path="/path/to/file/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);

I would assume that this sucks in the whole file, puts it into the variable $file, then determines its length and that's it. But that's not what happens. Here's the output I get on OSX:

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

So in fact the loop is executed twice! The exact same code runs just as expected under Linux, i.e. with the loop only being executed once. What is going on here?

Thanks,
Sebastian

PS: I'm using the pre-installed Perl 5.6.0. The only thing I added to the base installation is the CPAN module and what's needed for communicating with MySQL (DBI, Data::ShowTable, and DBD::mysql)
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Apr 30, 2001, 09:25 AM
 
(I'm sure there's a very good explanation for this, but I can't place it right now).

When you undef $/, you essentially make the input record sep. the NULL character (to my understanding). There's a NULL character at the end of your file, so I'm assuming it stops at that, then continues to the next (non-existent) character, returning a '0' length.

Try something like this, instead of undef $/...

$/ = \x04;
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
sebhoff  (op)
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Zurich, Switzerland
Status: Offline
Reply With Quote
Apr 30, 2001, 09:31 AM
 
Originally posted by parallax:


Try something like this, instead of undef $/...

$/ = \x04;
Yep, it works!
Thanks!
Sebastian



[This message has been edited by sebhoff (edited 04-30-2001).]
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:40 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2