in reply to how do I write directly to a file

Perl uses buffering on its filehandles; data is cached and written in 4K chunks. To turn that off, set the file handle to auto flush;

use IO::Socket; @colors = qw(pink yellow blue green); open(COLORS, ">>colors.txt") or die $!; # turn off stdio buffering on COLORS COLORS->autoflush(1); foreach $color (@colors){ print COLORS "$color\n"; print "sending $color to File\n"; } while (<STDIN>) { chomp; exit if ($_ eq "quit"); print COLORS $_; }

Replies are listed 'Best First'.
Re: Re: how do I write directly to a file
by davorg (Chancellor) on Jun 07, 2001 at 19:37 UTC

    The other way to turn off buffering is to set the $| variable to a true value. This effects the currently selected default output filehandle (generally STDOUT) but you can change it on other filehandles like this:

    my $fh = select COLORS; $|++; select $fh;

    or in slightly more scarey code, like this:

    select((select(COLORS), $| = 1)[0]);

    So you can see why the IO::Socket method is preferred :)

    --
    <http://www.dave.org.uk>

    Perl Training in the UK <http://www.iterative-software.com>