Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Strange piece of code!

by solegaonkar (Beadle)
on Jul 26, 2012 at 09:51 UTC ( [id://983814]=perlquestion: print w/replies, xml ) Need Help??

solegaonkar has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks! I am trying to debug a legacy perl application where I came across the following code
select((select(ERRFP), $| = 1)[0]);
ERRFP is a file handle created at the beginning of the function. This one fails my imagination! Any idea what is the special use of this kind of a construct? Is it doing anything other than this?
select(ERRFP); $| = 1;

Replies are listed 'Best First'.
Re: Strange piece of code!
by kcott (Archbishop) on Jul 26, 2012 at 10:07 UTC

    select returns the currently selected filehandle. So, this is doing what you appear to expect:

    select(ERRFP), $| = 1

    then, within its parentheses, it generates a list:

    (last_selected_filename, 1)

    Taking the first index of that list ((last_selected_filename, 1)[0]) gives:

    last_selected_filename

    Finally, this leaves you with:

    select(last_selected_filename);

    In short, you select ERRFP and make it unbuffered, then reselect the original filehandle. All in one line. Haven't seen this is ages. :-)

    Update: I just remembered where I did see this last: using format about a decade ago. See perlform - Format Variables for examples of less uglier ways to do the same thing.

    -- Ken

      Thank you Ken!
Re: Strange piece of code!
by davido (Cardinal) on Jul 26, 2012 at 09:58 UTC

    It selects ERRFP, sets that handle's autoflush mode to on, and then reselects the original filehandle.

    What it's missing is a # Comment.


    Dave

      Dave, Thanks for that. But that is what confuses me.. is there any value added by selecting ERRPF again?

        It's not selecting ERRPF again, it's reselecting the original filehandle... whatever was current before the first select (before ERRPF). select returns the filehandle that was current before the call.

        Read it from the inside out:

        1. select ERRPF and remember whatever was selected beforehand.
        2. Set autoflush on ERRPF
        3. Return to the outer select the first element in the list, which is the filehandle that we remembered in step one
        4. select that "remembered" filehandle.

        Dave

Re: Strange piece of code!
by Jenda (Abbot) on Jul 26, 2012 at 17:51 UTC

    Others already explained what it does, now for a more readable alternative.

    If you do not have time to change the FILEHANDLE to a lexical filehandle (or do not dare to make big changes) you can do this:

    use IO::Handle; ERRFP->autoflush();

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Well HANDLE->autoflush(1);

        Nope. The parameter is optional and the default is true: ... If no argument is given the previous value is unchanged (except for $io->autoflush will actually turn ON autoflush by default). - from perldoc IO::Handle

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Re: Strange piece of code! ( select((select(OUTPUT_HANDLE), $| = 1)[0]); )
by Anonymous Monk on Jul 26, 2012 at 20:12 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://983814]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-20 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found