in reply to Re: Interpolation of file handles in print
in thread Interpolation of file handles in print

Hello,

This will work but not with use strict :

#~ use strict ; use warnings ; my @marctags; my @tagdata ; $marctags[0]='FOO' ; $marctags[1]='BAR' ; open $marctags[0], ">foo.txt" or die "Failed"; open $marctags[1] , ">bar.txt" or die "Failed" ; $tagdata[0]="foo foo foo"; $tagdata[1]="bar bar bar"; for my $x (0..1) { print { $marctags[$x] } $tagdata[$x] ; }
In perldoc -f print it says :

use strict will raise a : Can't use string ("FOO") as a symbol ref while "strict refs" error that i don't know how to avoid here... So use FileHandle may be better :)

Replies are listed 'Best First'.
Re^3: Interpolation of file handles in print
by jhourcle (Prior) on May 17, 2005 at 11:36 UTC

    Sometimes, you have to tell perl that you really do know what you're doing, and that it's not a mistake. You don't want to shut off warnings or strict for the whole program, but if there's a specific section that perl complains about, that is what you want, you can do something like the following:

    { no strict qw(refs subs); print { $OK ? STDOUT : STDERR } "stuff\n"; }