Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^5: Code style question

by kcott (Archbishop)
on May 22, 2021 at 11:24 UTC ( [id://11132881]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Code style question
in thread Code style question

If you omit the filehandle argument in print, it will default to the last selected filehandle.

Unless you change it, you can expect the current filehandle to be STDOUT. In most cases, print @list and print STDOUT @list mean exactly the same thing; the STDOUT is typically omitted.

You probably won't need to do it very often; however, if you do change the filehandle (with select) you should keep track of the old filehandle, as you'll often need to restore it.

Here's a short script, with output file contents, to demonstrate:

$ perl -e ' print "Expecting STDOUT\n"; print STDOUT "Specifying STDOUT\n"; print STDERR "Specifying STDERR\n"; print "Current filehandle: ", select(), "\n"; my $orig_fh = select STDERR; print "New filehandle: ", select(), "\n"; print "Expecting STDERR (after select STDERR)\n"; select $orig_fh; print "Original filehandle: ", select(), "\n"; print "Expecting STDOUT (after select \$orig_fh)\n"; ' 1> x.out 2> x.err $ cat x.out Expecting STDOUT Specifying STDOUT Current filehandle: main::STDOUT Original filehandle: main::STDOUT Expecting STDOUT (after select $orig_fh) $ cat x.err Specifying STDERR New filehandle: main::STDERR Expecting STDERR (after select STDERR)

— Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found