in reply to Re: Syntax error when trying to use a hash value as a file stream specifier
in thread Syntax error when trying to use a hash value as a file stream specifier

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". ;-)

Replies are listed 'Best First'.
Re^3: Syntax error when trying to use a hash value as a file stream specifier
by BillKSmith (Monsignor) on Sep 03, 2022 at 18:41 UTC
    Good point! The function select acts as if it controls an anonymous global variable. That 'variable' has all the advantages and disadvantages of any global variable except that we cannot examine it. In your example, we all expect say to output to stdout. It is easy to forget that it may not be true.

    Until the documentation for say is corrected, the victim of this bug could certainly claim that the bug is in perl, not his code.

    Bill
      > we cannot examine it

      Of course we can:

      if (select eq 'main::STDERR') {

      > Until the documentation for say is corrected

      Done.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]