Did you consider redirecting your default output with select rather than passing it as an argument?
The handle is not passed as an argument, if it was, it would be followed by a comma. It is actually indirect object syntax (method $object @arguments in contrast to $object->method(@arguments)), see perlobj.
Using select is a global operation, it switches the default handle for all write, print, and (not yet documented in select) say <Update>without an explicit handle</Update> from STDOUT (or whatever was previously selected) to the handle passed to select. This has consequences that may lead to unwanted features a.k.a. bugs:
package Some::Third::Party::Module;
# ...
sub doSomething
{
say "This will take some time";
sleep 10;
say "Done";
}
# ...
#!/usr/bin/perl
# ...
use Some::Third::Party::Module qw( doSomething );
# ...
open my $h,'>:raw','output.bin';
my $oldSelected=select $h;
print "\x00\x42\xAF\x99";
doSomething();
print "\x33\xFF\x81\x73";
select $oldSelected;
close $h;
Guess why output.bin is longer than expected and does not work as expected.
Using the usual indirect object notation fixes the problem:
open my $h,'>:raw','output.bin';
print $h "\x00\x42\xAF\x99";
doSomething();
print $h "\x33\xFF\x81\x73";
close $h;
Of course, if you like the mental pain and want to get hurt by the future maintainer, you could also select handles as needed. Welcome back to the 1960s, when this was the only way to handle files in MUMPS:
open my $h,'>:raw','output.bin';
my $oldSelected=select $h;
print "\x00\x42\xAF\x99";
select $oldSelect;
doSomething();
select $h;
print "\x33\xFF\x81\x73";
select $oldSelected;
close $h;
I would not use select to globally switch file handles, not even for a short piece of code. It just causes way too much trouble.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
|