On Aug 23, 2005, at 12:34, Dan Klose wrote:

I have a script where I need to open a file, read from it sequentially,
and append items to the file as I go along, continuing to read these
items as I go. But seems I can't open the file for read and append at
the same time. Does anyone have any ideas?

Tie::File to the rescue!

See for instance this example (foo.pl):

    use strict;
    use warnings;

    use Tie::File;

    tie my @fh, "Tie::File", "foo.txt" or die $!;

    my $nlines = @fh;
    my $i = 0;
    foreach my $line (@fh) {
        push @fh, $line;
        last if ++$i == $nlines;
    }

and this shell session:

    % cat foo.txt
    foo
    bar
    % perl foo.pl
    % cat foo.txt
    foo
    bar
    foo
    bar

-- fxn

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to