Perl: Opening files for writing

Apple / MAC PRO
March 21, 2009 at 18:57:18
Specs: Mac OS X 10.5.6, Octo 2.8Ghz Xeon
Is there a way to open several files with one simple command?

I have a script that needs to create and write to 24 files. The following is
what I have, but would prefer to consolidate the 'opens', to tidy it up a bit.

open (OPRA_1, ">> $path/sym/occ_opra1.sym") || die "Error: $!\n";
open (OPRA_2, ">> $path/sym/occ_opra2.sym") || die "Error: $!\n";
open (OPRA_3, ">> $path/sym/occ_opra3.sym") || die "Error: $!\n";
open (OPRA_4, ">> $path/sym/occ_opra4.sym") || die "Error: $!\n";
open (OPRA_5, ">> $path/sym/occ_opra5.sym") || die "Error: $!\n";
open (OPRA_6, ">> $path/sym/occ_opra6.sym") || die "Error: $!\n";
open (OPRA_7, ">> $path/sym/occ_opra7.sym") || die "Error: $!\n";
open (OPRA_8, ">> $path/sym/occ_opra8.sym") || die "Error: $!\n";
open (OPRA_9, ">> $path/sym/occ_opra9.sym") || die "Error: $!\n";
open (OPRA_10, ">> $path/sym/occ_opra10.sym") || die "Error: $!\n";
open (OPRA_11, ">> $path/sym/occ_opra11.sym") || die "Error: $!\n";
open (OPRA_12, ">> $path/sym/occ_opra12.sym") || die "Error: $!\n";
open (OPRA_13, ">> $path/sym/occ_opra13.sym") || die "Error: $!\n";
open (OPRA_14, ">> $path/sym/occ_opra14.sym") || die "Error: $!\n";
open (OPRA_15, ">> $path/sym/occ_opra15.sym") || die "Error: $!\n";
open (OPRA_16, ">> $path/sym/occ_opra16.sym") || die "Error: $!\n";
open (OPRA_17, ">> $path/sym/occ_opra17.sym") || die "Error: $!\n";
open (OPRA_18, ">> $path/sym/occ_opra18.sym") || die "Error: $!\n";
open (OPRA_19, ">> $path/sym/occ_opra19.sym") || die "Error: $!\n";
open (OPRA_20, ">> $path/sym/occ_opra20.sym") || die "Error: $!\n";
open (OPRA_21, ">> $path/sym/occ_opra21.sym") || die "Error: $!\n";
open (OPRA_22, ">> $path/sym/occ_opra22.sym") || die "Error: $!\n";
open (OPRA_23, ">> $path/sym/occ_opra23.sym") || die "Error: $!\n";
open (OPRA_24, ">> $path/sym/occ_opra24.sym") || die "Error: $!\n";
open (OCC_RAW, $rawOccFile) || die "Error  opening the raw OCC file: $!\n";

Thanks in advance.


See More: Perl: Opening files for writing

Report •


#1
March 22, 2009 at 00:08:42
my $path = '/some/path';
my %FH;

for my $i (1..24) {
    open $FH{OPRA_$i}, '>>', "$path/sym/occ_opra$i.sym" or die "Error: $!";
}


Report •

#2
June 10, 2009 at 19:13:40
I forgot about this post. thanks.

I'm getting the following error upon execution:

Can't call method "OPRA_1" without a package or object	
reference

I'm lost on this one.

I've been searching around the web, but haven't really found anything that relative to file handles, really. Do you have any ideas?


Report •

Related Solutions


Ask Question