in reply to Re: how can i use one handle for multifple files
in thread how can i use one handle for multifple files

You can implify your code if you put your filehandles into a hash. The condition was a test for a word (TRUE or FALSE) so the hash would be:
$fh{'TRUE'}=$fh1; $fh{'FALSE'}=$fh2;
and the print woud simply be: print $fh{$test} "..." Update: Thanks to BrowserUK (see below)!
print { $fh{$test} } "..."

Replies are listed 'Best First'.
Re: Re: Re: how can i use one handle for multifple files
by rob_au (Abbot) on May 15, 2003 at 12:31 UTC
    No, that will not work - You cannot print directly to a file handle stored in anything other than a scalar because of the typeglob nature of file handles. Your code would return the following error as a result of the hash entry being interpreted as the first element of a list argument to the print function:

    String found where operator expected at test.perl line 10, near "} "te +st\n"" (Missing operator before "test\n"?) syntax error at test.perl line 10, near "} "test\n"" Execution of test.perl aborted due to compilation errors.

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001001011001"))'

      This can be worked around by wrapping your lexical file handles with curlies print {$fh} 'Stuff to print';

      Eg.

      D:\Perl\test>type temp2.pl8 #! perl -slw use strict; my @handles; open $handles[$_], '>', "test$_.dat" for 0 .. 3; print {$handles[$_]} 'Some text' for 0 .. 3; for( @handles ) { print {$_} 'Some more text'; } D:\Perl\test>temp2.pl8 D:\Perl\test>type test*.dat test0.dat Some text Some more text test1.dat Some text Some more text test2.dat Some text Some more text test3.dat Some text Some more text

      You can also use this to store your file handles in a hash which can be useful. You can even use an anonymous block to determine which handle to use at runtime. From perlfunc:print (>5.6?)

      Note that if you're storing FILEHANDLES in an array or other expressio +n, you will have to use a block returning its value instead: print { $files[$i] } "stuff\n"; print { $OK ? STDOUT : STDERR } "stuff\n";

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller