Chon-Ji has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to open multiple files all at the same time and store them into a sginle handle basically this is my pseudo code
@list #contains list filenames foreach @list open(HANDLE, @list[$index]); @data = <HANDLE>; #etc.... $index++; end for
Can I do away with the for loop and just open them all at once?

Replies are listed 'Best First'.
Re: Opening multiple files
by holli (Abbot) on May 30, 2006 at 08:07 UTC
    Short answer: No.
    Longer answer: No, and why do you want to do so? You can have multiple files open at the same time, but in your pseudocode there's no benefit of opening all files at once over opening them one after another. I suspect an XY Problem.


    holli, /regexed monk/
Re: Opening multiple files
by japhy (Canon) on May 30, 2006 at 11:03 UTC
    I'd be sneaky and use @ARGV and <> to my advantage:
    { local @ARGV = @list_of_files; while (<>) { ... } }

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Opening multiple files
by l.frankline (Hermit) on May 30, 2006 at 08:13 UTC

    Honestly, it smells like home work.

    map {open IN,"$_";undef $/; $all_files_data .= <IN>;close IN;} @files; print "$all_files_data";

    regards
    Franklin

    Don't put off till tomorrow, what you can do today.

      Well,what I'm trying to do is to remove the for loop and just have all the file contents under a single handles....again if that's possible. I'll try Franklin's suggestion and see how it works
Re: Opening multiple files
by BrentDax (Hermit) on May 30, 2006 at 08:21 UTC

    It's possible, but it's a real hack, involving @ARGV and the magic <> construct. (I won't tell you how to do it because if you're not clever enough to find it with those hints, you're not clever enough to know not to use it.) You're really better off simply using a loop. The correct way to code that loop would probably be like this:

    my @lines; foreach my $file(@files) { open(IN, "< $file") or die "Can't open $file: $!"; push @lines, <IN>; close IN; } # Now do whatever you want with @lines

    Alternatively, remove all references to @lines and replace the push line with:

    while(my $line = <IN>) { #Do whatever processing you want with $line }

    Hope this helps,

    =cut
    --Brent Dax
    There is no sig.

Re: Opening multiple files
by ambrus (Abbot) on May 30, 2006 at 11:42 UTC

    No, because perl can only have 256 handles open at once and they have to be named in capital letters and there are only 26 of those.

    If you want to open more files you can use ruby which has first-class filehandles

    @list #contains list filenames @handle = @list.map {|n| File.new(n) } for h in @handle @data = h.readlines end
    or gawk which hides filehandles away so you only need to handle filenames. (Update: you could use octave if you insist on the "end for" bit.)

    There's also a trick (ref. 483243 and 544184) to open multiple files: `cat filename1 filename2` but that only works in unix and has to read everything to memory so I don't recommend to do it in production code.

    (This reply is meant to be a bad joke. Don't take it seriously, instead read holli's reply. I think too that this is an XY problem. You can open multiple filehandles simultanously, using lexical filehandles (or Symbol). There's however no need for that if all you want is to read the data from all of them, as you can open the files sequentially: you can reuse a filehandle by reopening it (which will close the previous file).)