in reply to problem using seek on some systems

How about

my $seek_loc = (-s FILE) - $bytes; $seek_loc = 0 if $seek_loc < 0; seek(FILE, $seek_loc, 0);

By the way, you should use lexical variables and 3-arg open whenever possible.

open(my $fh, '<', $file_name) or die("Unable to read log file \"$file_name\": $!\n");

Replies are listed 'Best First'.
Re^2: problem using seek on some systems (portable)
by tye (Sage) on Dec 06, 2006 at 22:42 UTC

    There is no guarantee that -s and seek use identical values. On Unix, that is usually the case. Other places, not as much. If you want to get the seek position of the end of the file (portably), use seek then tell.

    - tye        

      Could you give us an example of when this might not be the case?

      Is this a units thing? unicode -v- bytes. Or a platform line ending thing?

Re^2: problem using seek on some systems
by lokiloki (Beadle) on Dec 06, 2006 at 22:00 UTC
    thank you very much sir. what's the benefit of using lexical variables and 3-arg opens over the old way of doing things?

      Lexical variables have a tighter scope than package variables. There are numerous advantages to limiting the scope of a variable. Predominantly, your handle will be closed if even if you don't execute the block to its end.

      Reasons not to use a lexical variable for a file handle: Supporting Perl versions older than 5.6.0.

      The 3-arg open can open files 2-arg open can't open. The 3-arg open is not subject to the injection bugs and attacks to which 2-arg open is vulnerable.

      Reasons not to 3-arg open. Wanting to open STDIN/STDOUT using '-'. Wanting to work file descriptors. Wanting to open a pipe.