Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Tear it apart...select()

by snafu (Chaplain)
on Mar 01, 2003 at 22:01 UTC ( [id://239770]=perlquestion: print w/replies, xml ) Need Help??

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

I've seen this a few times and yes, it is explained as "fancy" but will someone help me understand exactly what it's doing?

select( (select(STDERR), $|++)[0] );
I will take a stab at it. Select() simply select a filehandle to read and write to for the current process, right? But what exactly is the embedded select() doing??

It looks like the inside select() is returning element 0 of an array which the external select() is selecting.

So, I ran a couple of tests:

$ perl -e ' open(F,"/etc/passwd"); print select(F),"\n"; print select(STDOUT),"\n"; close(F); ' main::F $ perl -e ' open(F,"/etc/passwd"); print select((select(F),$|++)[0]),"\n"; print select(STDOUT),"\n";close(F); ' main::F main::STDOUT
It looks like I now have select()'ed two filehandles to play with at the same time. Is that right? TIA, - Jim

_ _ _ _ _ _ _ _ _ _
- Jim
Insert clever comment here...

Replies are listed 'Best First'.
Re: Tear it apart...select()
by Aristotle (Chancellor) on Mar 02, 2003 at 00:57 UTC
    Let's verbosify it:
    • select( (select(STDERR), $|++)[0] );
    • my @list = (select(STDERR), $|++); select(@list[0]);
    • my ($fh, $junk) = (select(STDERR), $|++); select($fh);
    • my $fh = select(STDERR); my $junk = $|++; select($fh);
    • my $fh = select(STDERR); $|++; select($fh);

    Makeshifts last the longest.

•Re: Tear it apart...select()
by merlyn (Sage) on Mar 02, 2003 at 04:07 UTC
    As the originator of that meme, let me apologize for that, right alongside my apology for originally promoting the use of grep in a void context purely for its side effects, and for originating the concept of a JAPH, which tends to lead non-Perl hackers to believe that Perl is only about obscurity.

    I'm sorry.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      I understand the apologies for grepping in void and the concept of the JAPH, but why do you apologize for the select idiom?

      Makeshifts last the longest.

        Well, the post was all tongue in cheek, but to break character here, the select() idiom was something I came up with one day when I was trying to unbuffer an arbitrary filehandle, regardless of the currently selected filehandle. Since I had invented the literal slice notation, I wondered if that could be used there to eliminate the normal temporary variable by holding the previous handle as part of the constructed list. And boom, there it was.

        The problem is that its obscurity offsets any useful gain over just writing it as a normal two or three step process.

        Another way to do it without using a "temporary" variable might be something like:

        for (select(HANDLETOUNBUFFER)) { $|++; select($_); }
        But I'd argue this is just as obscure.

        Moral of the story: it's ok to say my $old = select(NEW); when you need the old handle. It's fine. Don't worry. Be happy.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

      But that is one of the coolest things about Perl!!! =P It keeps me on my toes!

      _ _ _ _ _ _ _ _ _ _
      - Jim
      Insert clever comment here...

Re: Tear it apart...select()
by Chmrr (Vicar) on Mar 01, 2003 at 22:08 UTC

    select changes the current filehandle, and returns the old one. So that construct changes the current filehandle to STDERR, unbuffers it (with $|++), then selects the 0th element of that list -- which happens to be the return value of the select, which is the original filehandle. That filehandle is then selected.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-12-07 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.