"David Storrs" <[EMAIL PROTECTED]> wrote
>        # Print file, inefficiently
>     print $default.readline for 1..$default.lines;

print it efficiently:

  print $default;

>     # Append a line
>     $rw .= "an additional line\n";

$rw ~= "\n" unless $rw.chars[-1] eq "\n";
$rw ~= "an additional line\n";

>     # New line is visible through $rw
>     print $rw.lines[-1];  # (*)
>
>     # last line not yet visible through $default because it was
>             # added by external handle--just like in a tail -f, we
>             # need to move file pointer manually
>     $default.seek(File.current_pos);

I don't think the manual sync is really needed: name the method something a
bit more neutral:

  $default.refresh;

> (*) The .lines[-1] semantic is feasible if we are willing to tolerate
> very slow performance (at least the first time; it could cache the
> locations of $/ after scanning and dump the cache if $/ changes), the
> fact that it wouldn't work on all files (/dev/null, /dev/zero, etc),
> and a few other issues.

The performance here could depend on the encoding. If the file is ASCII,
then we don't need to worry about multi-byte characters, so the
under-the-hood implementation could follow a heuristic such as "seek to 1000
bytes from end of file: scan forward for "\n". If none found, then go back
further. Otherwise continue to scan to find last "\n" in the file".

Dave.


Reply via email to