Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Behavior change in 5.6??

by athomason (Curate)
on Apr 12, 2001 at 03:57 UTC ( #71908=note: print w/replies, xml ) Need Help??

in reply to Behavior change in 5.6??

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.

Replies are listed 'Best First'.
Re: Re: Behavior change in 5.6??
by mdchachi (Initiate) on Apr 12, 2001 at 06:24 UTC
    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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://71908]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (None)
    As of 2021-10-19 02:34 GMT
    Find Nodes?
      Voting Booth?
      My first memorable Perl project was:

      Results (76 votes). Check out past polls.