Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Behavior change in 5.6??

by mdchachi (Initiate)
on Apr 12, 2001 at 03:10 UTC ( [id://71893]=perlquestion: print w/replies, xml ) Need Help??

mdchachi has asked for the wisdom of the Perl Monks concerning the following question:

Greetings. I have a simple script which tries to save my html log before my ISP discards it. It looks up the last line in my saved file, finds that line in the html file and appends to my saved file from that point. It works fine on version "5.005_03 built for i386-linux" but on a different machine running "v5.6.0 built for i686-linux" it stops and waits for standard input at one point. If I press enter once it then completes. What is going on? I am not passing any arguments to my script whatsoever. Here is the script. The stopping point is indicated. Many thanks.
#!/usr/local/bin/perl $home="/home/epcom"; $log="$home/access-mc"; $savelog="$home/log/access-mc"; open (LOGF,"$log") || die "[45]Open Error $log"; open (SAVEF,">>$savelog") || die "[45]Open Error $savelog"; close (SAVEF); open (SAVEF,"$savelog"); while (<SAVEF>) { # !!!it waits for standard input here, at the eof() call # but just once $last=$_ if eof(); } close (SAVEF); $lineno = 0; if ($last =~ /^....*/) { chop $last; while (<LOGF>) { if (m/\Q$last/) { $lineno = $.; $lineno = $.; } } } close (LOGF); open (SAVEF,">>$savelog"); open (LOGF,"$log"); while (<LOGF>) { print SAVEF if ($. > $lineno && ! /\.jpg|\.gif/ ) ; } close (SAVEF); close (LOGF); exit 0;

Edit: chipmunk 2001-04-11

Replies are listed 'Best First'.
Re: Behavior change in 5.6??
by athomason (Curate) on Apr 12, 2001 at 03:57 UTC
    Take a closer look at the the eof docs. Using the form eof() uses the same magic as while (<>) to pull input from STDIN if no filenames are passed on the command line. Using eof() is best reserved for use only inside the while (<>) construct for that same reason. Why the behavior is different in 5.6, I'm not sure, but this is documented at least back to 5.004_04.

    The simplest solution is to remove the parentheses from the eof call, which will cause the builtin to default to the last filehandle read. The other solution is to name the filehandle explicitly. The latter is a better habit, anyhow; if file-handling code were to sneak in between your last read and the call to eof, the no-parens form would use the other handle instead of SAVEF as you intended.

    As an aside, are you sure you need to be using eof at all? Perl's input operators return undef when they run out of input; eof is more of a C holdover than a critical function. Your while loop looks rather strange (perhaps because you didn't use <code> tags--I think you meant while (<SAVEF>)), so it's hard to tell what you were intending. If you're trying to get at the last line of the file, see this FAQ.

    Update: Expanded upon reply.

      Thank you. I do want to note that my code didn't show up properly and the while() isn't empty as it seems. However, you, being a wise perl wisdom-giver have correctly figured out what it actually contains. Just for grins, here it is again with the code tags:
      #!/usr/local/bin/perl $home="/home/epcom"; $log="$home/access-mc"; $savelog="$home/log/access-mc"; open (LOGF,"$log") || die "[45]Open Error $log"; open (SAVEF,">>$savelog") || die "[45]Open Error $savelog"; close (SAVEF); open (SAVEF,"$savelog"); while (<SAVEF>) { $last=$_ if eof(); # holding me up until Enter is pressed at the eof() } close (SAVEF); $lineno = 0; if ($last =~ /^....*/) { chop $last; while (<LOGF>) { if (m/\Q$last/) { $lineno = $.; } } } close (LOGF); open (SAVEF,">>$savelog"); open (LOGF,"$log"); while (<LOGF>) { print SAVEF if ($. > $lineno && ! /\.jpg|\.gif/ ) ; } close (SAVEF); close (LOGF); exit 0;
      Thanks very much. I will follow up on your excellent suggestions and no doubt get the script working soon. Using eof() was the only way I knew to get the last line of the file. Thanks for pointing me to the better methods.
Re: Behavior change in 5.6??
by Beatnik (Parson) on Apr 12, 2001 at 12:30 UTC
    not really the problem, but define an access mode... check this

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://71893]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found