in reply to Can't open file within subroutine

Ok I cleaned it up a bit, I sill have the same problem though, only now I also have an uniitalized value for INFO. The file is still not being read. <CODE> "#!/usr/bin/perl -w opendir (EXA, "/export/home/cad/data/products") || die "no dir?: $1"; foreach $name(sort readdir (EXA)) { print "$name\n"; open(INFO, <"/export/home/cad/data/products/$name/source/$name.bom">) || die "no dir...grrrr?: $1"; @lines = <INFO> ; } foreach $line (@lines) { $line=~ s/BOARDPLACEMENT_BOM brd//; print "\n $line "; print "\n ------------------------ \n"; }" <CODE>

Replies are listed 'Best First'.
RE: Re: Can't open file within subroutine
by muppetBoy (Pilgrim) on Jun 14, 2000 at 18:33 UTC
    Try replacing
    open(INFO, <"/export/home/cad/data/products/$name/source/$name.bom">)
    with
    open(INFO, "/export/home/cad/data/products/$name/source/$name.bom")
    This should work if you want to just read from INFO.
    You are still not actually doing anything with the contents of the file. Try using:
    @lines = <INFO>; close (INFO);
    this will read all of the file into the lines array and then close the file (which is always good practice)
    Also, in your open or die code you may want to use $! as that will contain the relevent error message.
    Incidently try to wrap any code you post in the code tags, see Site How To, because it make it much easier to read.
RE: Re: Can't open file within subroutine
by t0mas (Priest) on Jun 14, 2000 at 18:48 UTC
    I'm not really sure what you are trying to do but here's a guess:
    #!/usr/bin/perl opendir(DIR, '/export/home/cad/data/products') || die "Can't opendir D +ir: $!"; my @Dirs = grep { /^[^.].*/ && -d "/export/home/cad/data/products/$_" +} readdir(DIR); closedir DIR; foreach $name (@Dirs) { open(FILE,"</export/home/cad/data/products/$name/source/$name.bom" +) || die "Can't open $file: $!"; my @lines=<FILE>; close(FILE); foreach $line (@lines) { $line=~ s/BOARDPLACEMENT_BOM brd//; print "\n $line "; print "\n------------------------ \n"; } };
    The thing I can't really figure out is why anyone would like to do that.

    /brother t0mas