what does “select((select(STDOUT), $| = 1)[0]);” in Perl

The way to figure out any code is to pick it apart. You know that stuff inside parentheses happens before stuff outside. This is the same way you’d figuring out what code is doing in other languages.

The first bit is then:

( select(s), $|=1 )
That list has two elements, which are the results of two operations: one to select the s filehandle as the default then one to set $| to a true value. The $| is one of the per-filehandle variables which only apply to the currently selected filehandle (see Understand global variables at The Effective Perler). In the end, you have a list of two items: the previous default filehandle (the result of select), and 1.

The next part is a literal list slice to pull out the item in index 0:

( PREVIOUS_DEFAULT, 1 )[0]
The result of that is the single item that is previous default filehandle.

The next part takes the result of the slice and uses it as the argument to another call to select

select( PREVIOUS_DEFAULT );
So, in effect, you’ve set $| on a filehandle and ended up back where you started with the default filehandle.

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: