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

后端 未结 7 680
执念已碎
执念已碎 2020-12-03 01:19

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 netwo

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 01:40

    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.

提交回复
热议问题