kokakola613 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to write a script that parses a file, which itself contains file names. I want to open each one of these files listed and perform some data analysis. However, I'm getting an error any way I try to do it.

Here is my original code (adapted)

open(INP, "<$file_list") or die("Error reading file"); while (<INP>) { my $data_file = m/(\w+)/; # I then call a subroutine which opens the $data_file # and performs some analysis on it }

I wasn't surprised the above didn't work, since INP would have to be closed for the other file to be read in the subroutine I called. However, the following did not work also:

open(INP, "<$file_list") or die("Error reading file"); @lines = <INP>; while (@lines) { my $data_file = m/(\w+)/; # I then call a subroutine which opens the $data_file # and performs some analysis on it }

I then get an error on uninitialized values. I could slurp the entire file into a string, and then use split to get the lines, but that seems like it would be costly. Any ideas?

Replies are listed 'Best First'.
Re: Multiple files opened
by ahmad (Hermit) on Sep 24, 2010 at 05:28 UTC

    Why do you need regex to read lines? my $data_file = m/(\w+)/; doesn't make sense to me

    Here's how you write it :

    open(INP, "<$file_list") or die("Error reading file"); while (my $data_file = <INP>) { chomp($data_file); # then call a subroutine which opens the $data_file # and performs some analysis on it }

      To expand on this:

      my $data_file = m/(\w+)/; assigns a true or false value to the variable indicating whether the regular expression matched. To save the captured value, assign the return value of m// from list context, not scalar context.

      But if the desired result is to remove the newline, use chomp.

Re: Multiple files opened
by Anonymous Monk on Sep 24, 2010 at 05:59 UTC
    INP would have to be closed for the other file to be read in the subroutine

    Why? The subroutine does not need to use the same file handle name INP, and you can certainly have two files open at the same time. There are limits, but 2 is reasonable. Just make sure you close the file handles - lexical handles will help with that, as it will with file handle name collisions:
    open(my $inp, '<', $file_list) or die("Error reading $file_list: $!"); while (<$inp>) { ... } close $inp;
    I also don't get why you are using an RE rather than chomp. Anyway, your assignment to $data_file probably does not do what you think it does - did you trty printing it out?

      kokakola613

      If you provide the "$file_list" few contents and show your subroutine piece of code, then Monks would be able to give their suggestions efficiently.

      Please show that.

Re: Multiple files opened
by changma_ha (Sexton) on Sep 24, 2010 at 08:53 UTC

    Hi kokakola613... "I wasn't surprised the above didn't work, since INP would have to be closed for the other file to be read in the subroutine I called. " . I don't think you need to close INP since Perl is intelligent to close it automatically. Also, as ahmad said .you don't need REX to read line...