Alex Rivera | Logout

What does "select((select(s),$|=1)[0])" do in Perl?

Asked 2008-10-13T05:00:53.507
35

I've seen some horrific code written in Perl, but I can't make head nor tail of this one:

select((select(s),$|=1)[0])

It's in some networking code that we use to communicate with a server and I assume it's something to do with buffering (since it sets $|).

But I can't figure out why there's multiple select calls or the array reference. Can anyone help me out?

Edit
Report

1 Answer

21
select($fh)

Select a new default file handle. See http://perldoc.perl.org/functions/select.html

(select($fh), $|=1)

Turn on autoflush. See http://perldoc.perl.org/perlvar.html

(select($fh), $|=1)[0]

Return the first value of this tuple.

select((select($fh), $|=1)[0])

select it, i.e. restore the old default file handle.


Equivalent to

$oldfh = select($fh);
$| = 1;
select($oldfh);

which means

use IO::Handle;
$fh->autoflush(1);

as demonstrated in the perldoc page.

answered 2010-02-18T09:39:19.777

Your Answer