in reply to Interpolation of file handles in print

You could do something like the following:
use FileHandle; my @files = ("file1", "file2", "file3", "file4", "file5", "file6", "f +ile7"); my @handles = map { my $fh = new FileHandle; $fh->open("< $_") ? $fh : undef; } @files; for $x (0..6) { my $h = $handles[$_]; print $h "$marctags[$x][3] $tagdata[$x]"; }
(untested)


holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: Interpolation of file handles in print
by ZlR (Chaplain) on May 17, 2005 at 10:35 UTC
    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 :
      Note that if you're storing FILEHANDLES in an array or other expression, you will have to use a block returning its value instead:

      print { $files[$i] } "stuff\n"; print { $OK ? STDOUT : STDERR } "stuff\n";

    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 :)

      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"; }
Re^2: Interpolation of file handles in print
by aukjan (Friar) on May 17, 2005 at 10:27 UTC