sriram83.life has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, What is the meaning of $| in perl because, i am unable to understand these two lines:

select(LOGFILE); $| = 1;

select(STDOUT);

Thanks,

Sriram

Replies are listed 'Best First'.
Re: what is the meaning of $| in perl?
by Corion (Patriarch) on Mar 10, 2014 at 11:41 UTC

    See perlvar, where many special Perl variables are explained. Also see select.

Re: what is the meaning of $| in perl?
by Discipulus (Canon) on Mar 10, 2014 at 11:45 UTC
    Hello, you'll find very intersting this one and that too

    HtH
    L*
    PS consider wrap ypur code in tags as stated here
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: what is the meaning of $| in perl?
by davido (Cardinal) on Mar 10, 2014 at 14:08 UTC

    Everyone should read perlintro when getting started with Perl, and refer back to it periodically, as it answers many basic questions and provides links to information for more in-depth ones. In this case, perlintro provides a link:

    There are a number of "magic" scalars with names that look like punctuation or line noise. These special variables are used for all kinds of purposes, and are documented in perlvar.

    And everyone posting here at PerlMonks should read Writeup Formatting Tips.


    Dave

Re: what is the meaning of $| in perl? (Buffering/autoflush/Unicode/UTF-8 References)
by eyepopslikeamosquito (Archbishop) on Mar 10, 2014 at 19:13 UTC
Re: what is the meaning of $| in perl?
by kcott (Archbishop) on Mar 10, 2014 at 22:15 UTC

    G'day Sriram,

    i am unable to understand these two lines:
    select(LOGFILE); $| = 1; select(STDOUT);

    The documentation for select has an example which is almost identical to the code you posted. It also has an example of autoflush() that was mentioned earlier in this thread.

    If you familiarise yourself with the documentation available from http://perldoc.perl.org/perl.html, you'll get answers a lot quicker than you will by posting a question here and then waiting for replies.

    One thing, that perhaps is not immediately apparent from the perlvar documentation, is that $| only has two states:

    $ perl -le 'print $|; $| = 999; print $|; $| = ""; print $|' 0 1 0

    -- Ken

Re: what is the meaning of $| in perl?
by toolic (Bishop) on Mar 10, 2014 at 15:06 UTC
    Also, from your command line (YMMV due to shell):
    perldoc -v '$|'
Re: what is the meaning of $| in perl?
by Laurent_R (Canon) on Mar 10, 2014 at 20:05 UTC
Re: what is the meaning of $| in perl?
by vinoth.ree (Monsignor) on Mar 11, 2014 at 08:52 UTC

    Whenever you print data on a filehandle, it will be buffered and the data will not be availale in the file, until the buffer get full.

    In Perl, we can not turn the buffering off, but you can get the same benefits by making the filehandle hot.

    Here in you code select(LOGFILE); make the LOGFILE filehandle to be selected and $|=1, which is the special variable in perl, If you set it to a true value, it makes the current filehandle hot. So Perl will flush the data in the buffer immediately once you print on that filehandle.select(LOGFILE); select will return the currect filehandle.


    All is well