Professional Web Applications Themes

FW: special vars - PERL Beginners

> -----Original Message----- > From: Nilay Puri, Noida > Sent: Wednesday, February 04, 2004 11:37 AM > To: Perl (E-mail) > Subject: FW: special vars > > > > -----Original Message----- > From: Nilay Puri, Noida > Sent: Wednesday, February 04, 2004 11:32 AM > To: Perl (E-mail) > Subject: special vars > > Hi all, > > Can any one help me understand the usage of special variable $| ? > > I know the description of this var. If set to nonzero, forces a flush > after every write or print. > > But I am not able ...

  1. #1

    Default FW: special vars


    > -----Original Message-----
    > From: Nilay Puri, Noida
    > Sent: Wednesday, February 04, 2004 11:37 AM
    > To: Perl (E-mail)
    > Subject: FW: special vars
    >
    >
    >
    > -----Original Message-----
    > From: Nilay Puri, Noida
    > Sent: Wednesday, February 04, 2004 11:32 AM
    > To: Perl (E-mail)
    > Subject: special vars
    >
    > Hi all,
    >
    > Can any one help me understand the usage of special variable $| ?
    >
    > I know the description of this var. If set to nonzero, forces a flush
    > after every write or print.
    >
    > But I am not able to understand its importance.
    >
    > If I am reading a file and writing the contents in a separat file after
    > some processing.
    > What will happen if I set $|=1 ?
    >
    > Thanks,
    > NP
    >
    >
    >
    >
    Nilay Puri Guest

  2. #2

    Default Re: FW: special vars

    On Feb 4, Nilay Puri, Noida said:
    >> Can any one help me understand the usage of special variable $| ?
    >>
    >> I know the description of this var. If set to nonzero, forces a flush
    >> after every write or print.
    Most filehandles buffer their output until a newline is reached. For
    example, try this code:

    for (1 .. 5) {
    print STDOUT;
    sleep 1;
    }

    At the end of 5 seconds, you'll get "12345". Compare that with this:

    for (1 .. 5) {
    print STDERR;
    sleep 1;
    }

    Here, you get 1, then a pause, 2, then a pause, and so on. STDOUT is
    buffered, whereas STDERR is not. Now, if you want to UNbuffer STDOUT, you
    set $| to 1.

    $| = 1;
    for (1 .. 5) {
    print STDOUT;
    sleep 1;
    }

    Now you get 1, pause, 2, pause, 3, pause, 4, pause, 5, pause.

    $| has its own value for each filehandle. In order to make filehandle FOO
    unbuffered, you need to select() FOO and then set $| to 1.

    my $old_fh = select FOO;
    $| = 1; # now FOO is unbuffered
    select $old_fh; # go back to whatever filehandle was previous selected

    --
    Jeff "japhy" Pinyan [email]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  3. #3

    Default Re: FW: special vars

    Nilay Puri wrote:
    >
    >> -----Original Message-----
    >>From: Nilay Puri, Noida
    >>Sent: Wednesday, February 04, 2004 11:37 AM
    >>To: Perl (E-mail)
    >>Subject: FW: special vars
    >>
    >>
    >>
    >> -----Original Message-----
    >>From: Nilay Puri, Noida
    >>Sent: Wednesday, February 04, 2004 11:32 AM
    >>To: Perl (E-mail)
    >>Subject: special vars
    >>
    >>Hi all,
    >>
    >>Can any one help me understand the usage of special variable $| ?
    >>
    >>I know the description of this var. If set to nonzero, forces a flush
    >>after every write or print.
    >>
    >>But I am not able to understand its importance.
    >>
    >>If I am reading a file and writing the contents in a separat file after
    >>some processing.
    >>What will happen if I set $|=1 ?
    >>
    >>Thanks,
    >>NP
    >>
    >>
    >>
    >>
    if u use an OS like linux (which will not write things imediately to
    disc) this forces it to do so.
    Eternius Guest

  4. #4

    Default AW: FW: special vars

    It is important believe me.

    Especialy if you are writting to logfiles, databases and unix named pipes.

    If in this context buffered io is on it will happen that if you prints
    something to
    a file or anywhere not the whole line is dumped only the stuff what was in
    the buffer
    when the buffer was flushed because of its size the last time.

    if you give out the line:
    Hello, I am here./n

    Buffered it could look like this at the destination:
    Hello, I a

    Unbuffered it will look like this:
    Hello, I am here./n

    Beware THERE IS A DIFFERENCE OF PRINTING TO A FILE, PIPE, PROGRAM and a TTY
    / COMMANDLINE.


    See Fcntl, File::Handle, Io::Handle at cpan for more.


    -----Ursprungliche Nachricht-----
    Von: Eternius [mailto:eterniusopposoft.de]
    Gesendet: Mittwoch, 4. Februar 2004 12:00
    An: [email]beginnersperl.org[/email]
    Betreff: Re: FW: special vars


    Nilay Puri wrote:
    >
    >> -----Original Message-----
    >>From: Nilay Puri, Noida
    >>Sent: Wednesday, February 04, 2004 11:37 AM
    >>To: Perl (E-mail)
    >>Subject: FW: special vars
    >>
    >>
    >>
    >> -----Original Message-----
    >>From: Nilay Puri, Noida
    >>Sent: Wednesday, February 04, 2004 11:32 AM
    >>To: Perl (E-mail)
    >>Subject: special vars
    >>
    >>Hi all,
    >>
    >>Can any one help me understand the usage of special variable $| ?
    >>
    >>I know the description of this var. If set to nonzero, forces a flush
    >>after every write or print.
    >>
    >>But I am not able to understand its importance.
    >>
    >>If I am reading a file and writing the contents in a separat file after
    >>some processing.
    >>What will happen if I set $|=1 ?
    >>
    >>Thanks,
    >>NP
    >>
    >>
    >>
    >>
    if u use an OS like linux (which will not write things imediately to
    disc) this forces it to do so.

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribeperl.org[/email]
    For additional commands, e-mail: [email]beginners-helpperl.org[/email]
    <http://learn.perl.org/> <http://learn.perl.org/first-response>



    Bastian Angerstein Guest

  5. #5

    Default Re: FW: special vars


    > Nilay Puri wrote:
    >
    >
    > if u use an OS like linux (which will not write things imediately to
    > disc) this forces it to do so.
    >
    That is misleading and not necessarily true. It tells Perl to unbuffer
    the I/O but not the OS. The OS decides what will and won't get written
    to disc, based on kernel settings, harddrive settings, etc. Don't
    confuse the two...

    [url]http://danconia.org[/url]

    Wiggins D Anconia Guest

  6. #6

    Default Re: FW: special vars

    then I guess, I didn't understand it myself
    Wiggins D Anconia wrote:
    >
    >>Nilay Puri wrote:
    >>
    >
    >
    >>if u use an OS like linux (which will not write things imediately to
    >>disc) this forces it to do so.
    >>
    >
    >
    > That is misleading and not necessarily true. It tells Perl to unbuffer
    > the I/O but not the OS. The OS decides what will and won't get written
    > to disc, based on kernel settings, harddrive settings, etc. Don't
    > confuse the two...
    >
    > [url]http://danconia.org[/url]
    >
    Eternius Guest

  7. #7

    Default Re: FW: special vars

    Eternius wrote:
    >
    > Wiggins D Anconia wrote:
    >
    > >>Nilay Puri wrote:
    > >>
    > >
    > >
    > >>if u use an OS like linux (which will not write things imediately to
    > >>disc) this forces it to do so.
    > >>
    > >
    > >
    > > That is misleading and not necessarily true. It tells Perl to unbuffer
    > > the I/O but not the OS. The OS decides what will and won't get written
    > > to disc, based on kernel settings, harddrive settings, etc. Don't
    > > confuse the two...
    > >
    > > [url]http://danconia.org[/url]
    > >
    >
    > Then I guess, I didn't understand it myself.
    Disk drives have their own cache. CPUs now have their own first, second and
    third level memory caching. The file system is likely to have two caches of
    its own per file, and the language at least one more in RAM.

    Disabling output buffering in Perl should be seen as a nicety that helps
    debugging, but not much else. Only a hardware solution can guard against
    losing power at the wrong time.

    Rob


    Rob Dixon Guest

  8. #8

    Default Re: special vars


    On Feb 4, 2004, at 10:23 AM, Rob Dixon wrote:
    [..]
    > Disabling output buffering in Perl should be seen as
    > a nicety that helps debugging, but not much else. Only
    > a hardware solution can guard against losing power at the wrong time.
    Minor Nit, yes I know that the thread has
    been about disk and/or filesystem I/O - but
    the need to disable output buffering becomes
    very important when one is doing client-server
    code over a socket and/or pipe.

    Nothing like having one side waiting for the otherside
    to do something - but the message did not get sent because
    it is sitting in a buffer waiting to be flushed...


    ciao
    drieux

    ---

    Drieux Guest

Similar Threads

  1. php vars into flash
    By kysh in forum Macromedia Flash Data Integration
    Replies: 1
    Last Post: January 20th, 05:00 PM
  2. Flash gets php vars but php doesn't get flash vars
    By neurotick webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 2
    Last Post: January 21st, 04:30 PM
  3. #26050 [Opn->Bgs]: Unable to use CONST vars in other CONST vars
    By sniper@php.net in forum PHP Development
    Replies: 0
    Last Post: October 31st, 01:41 PM
  4. #26050 [NEW]: Unable to use CONST vars in other CONST vars
    By kris dot hofmans at pandora dot be in forum PHP Development
    Replies: 0
    Last Post: October 31st, 10:24 AM
  5. our vs. use vars
    By Dan Muey in forum PERL Beginners
    Replies: 13
    Last Post: October 29th, 03:35 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139