[comp.lang.perl] Help : Array of FILEHANDLEs

merlyn@iwarp.intel.com (Randal L. Schwartz) (01/25/91)

In article <CA.91Jan24135758@saryu.usa>, ca@saryu (Cengiz Alaetinoglu) writes:
| 
| I am new to perl. I am trying to open arbitrary no of files, and write to each
| of them. What I wanna do is like
| 
| #!/usr/local/bin/perl
| 
| for ($i=0; $i < 5; $i++) {
|       open($files[$i], "| foo");
|       printf $files[$i] "Hello world \n";
| }

Hmm.  We were just discussing this.  You need either a filehandle or a
simple scalar after the print/printf.  And, you also need a named
filehandle for each name.

for ($i=0; $i < 5; $i++) {
	$handle = "FILE$i";
	open($handle, "| foo");
	printf $handle "Hello world \n";
}

This makes 6 filehandles named FILE0 through FILE6.  You can use those
directly later, as in:

	print FILE4 "some more for four\n";

And, unless you are doing "%"-style escapes, you probably want just a
print, not a printf.

eval q#die "Just another Perl hacker,\n"#; print $@
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/