How to flush to the file each time I write a record to a file? 
Author Message
 How to flush to the file each time I write a record to a file?

Anyone knows how to flush each time I write a record to a file?


Sat, 01 Dec 2001 03:00:00 GMT  
 How to flush to the file each time I write a record to a file?
Anyone can help?


Sat, 01 Dec 2001 03:00:00 GMT  
 How to flush to the file each time I write a record to a file?

Quote:

> Anyone knows how to flush each time I write a record to a file?

RTM "perldoc perlvar" about "$|".

--

    http://www.engelschall.com/u/sb/whoami/
    http://www.engelschall.com/u/sb/download/
    http://www.perl.com/CPAN/authors/id/STBEY/
    http://www.oreilly.de/catalog/perlmodger/bnp/



Sat, 01 Dec 2001 03:00:00 GMT  
 How to flush to the file each time I write a record to a file?
     [courtesy cc of this posting mailed to cited author]

In comp.lang.perl.misc,

:Anyone knows how to flush each time I write a record to a file?

What part of the very first question in the standard perlfaq5
manpage distributed with perl requires further clarification
before you won't ask this question again? :-(

    NAME
        perlfaq5 - Files and Formats ($Revision: 1.38 $, $Date:
        1999/05/23 16:08:30 $)

    DESCRIPTION
        This section deals with I/O and the "f" issues: filehandles,
        flushing, formats, and footers.

    How do I flush/unbuffer an output filehandle?  Why must I do this?

        The C standard I/O library (stdio) normally buffers characters
        sent to devices. This is done for efficiency reasons, so that
        there isn't a system call for each byte. Any time you use
        print() or write() in Perl, you go though this buffering.
        syswrite() circumvents stdio and buffering.

        In most stdio implementations, the type of output buffering and
        the size of the buffer varies according to the type of device.
        Disk files are block buffered, often with a buffer size of more
        than 2k. Pipes and sockets are often buffered with a buffer size
        between 1/2 and 2k. Serial devices (e.g. modems, terminals) are
        normally line-buffered, and stdio sends the entire line when it
        gets the newline.

        Perl does not support truly unbuffered output (except insofar as
        you can `syswrite(OUT, $char, 1)'). What it does instead support
        is "command buffering", in which a physical write is performed
        after every output command. This isn't as {*filter*} your system as
        unbuffering, but does get the output where you want it when you
        want it.

        If you expect characters to get to your device when you print
        them there, you'll want to autoflush its handle. Use select()
        and the `$|' variable to control autoflushing (see the section
        on "$|" in the perlvar manpage and the "select" entry in the
        perlfunc manpage):

            $old_fh = select(OUTPUT_HANDLE);
            $| = 1;
            select($old_fh);

        Or using the traditional idiom:

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

        Or if don't mind slowly loading several thousand lines of module
        code just because you're afraid of the `$|' variable:

            use FileHandle;
            open(DEV, "+</dev/tty");      # ceci n'est pas une pipe
            DEV->autoflush(1);

        or the newer IO::* modules:

            use IO::Handle;
            open(DEV, ">/dev/printer");   # but is this?
            DEV->autoflush(1);

        or even this:

            use IO::Socket;               # this one is kinda a pipe?
            $sock = IO::Socket::INET->new(PeerAddr => 'www.perl.com',
                                          PeerPort => 'http(80)',
                                          Proto    => 'tcp');
            die "$!" unless $sock;

            $sock->autoflush();
            print $sock "GET / HTTP/1.0" . "\015\012" x 2;
            $document = join('', <$sock>);
            print "DOC IS: $document\n";

        Note the bizarrely hardcoded carriage return and newline in
        their octal equivalents. This is the ONLY way (currently) to
        assure a proper flush on all platforms, including Macintosh.
        That's the way things work in network programming: you really
        should specify the exact bit pattern on the network line
        terminator. In practice, `"\n\n"' often works, but this is not
        portable.

        See the perlfaq9 manpage for other examples of fetching URLs
        over the web.

--
f u cn rd ths, u cn gt a gd jb n cmptr prgrmmng.



Sat, 01 Dec 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. writing on certain records in a file

2. Writing fixed-length records to file

3. How write records to ASCII file

4. Reading/Writing from text file at same time

5. Open file 2 times writing then append

6. How to Read/Block/Write a file at same time

7. greping records from file through file - code fix

8. PERLFUNC: localtime - convert UNIX time into record or string using local time

9. PERLFUNC: gmtime - convert UNIX time into record or string using Greenwich time

10. PERLFUNC: localtime - convert UNIX time into record or string using local time

11. PERLFUNC: gmtime - convert UNIX time into record or string using Greenwich time

12. file parse into hash and writing output with certain format to new file

 

 
Powered by phpBB® Forum Software