in reply to Can't open file within subroutine
Not only is this a little cleaner, and it gets rid of the troublesome nested subroutine, it avoids reading a potentially large file into memory. If you're just going to iterate over each line later, why use an array? (Acceptible answer, "When you need to open and close the file as quickly as possible.")#!/usr/bin/perl -w use strict; local *EXA; local *INFO; opendir (EXA, "/export/home/cad/data/products") || die "no dir?: $!"; foreach my $name(sort readdir (EXA)) { print "$name\n"; open(INFO, "/export/home/cad/data/products/$name/source/$name.bom" +) || die "no dir: $!"; while (my $line = <INFO>) { $line=~tr/(//d; #Cut out all the exa garbage $line=~ s/COMP//; $line=~ s/BOARDPLACEMENT_BOM brd//; print "\n $line "; } close(INFO) ; print "\n ------------------------ \n"; } closedir(EXA);
Explanation of the local: I like declaring typeglobs, it's fun. If you're really particular, wrap the whole bit in a block, from before the local statements to after the close statement, and this can be part of a bigger program that might also have filehandles named EXA and INFO. You won't clobber them here.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: RE: Can't open file within subroutine
by jlp (Friar) on Jun 15, 2000 at 15:26 UTC |