in reply to Read from directory list...

Use globbing to obtain a subset of the directory, then collect the content in a list.
#!/usr/bin/perl -w my @files = <*.pl>; # get list of .pl files in this dir my @content; foreach my $file (@files) { if (-f $file && open(FH,"<","$file")) { @content= (@content, <FH>); # add filecontent close(FH); } } # optionally: chomp @content print "$_" foreach @content;

Replies are listed 'Best First'.
Re^2: Read from directory list...
by johngg (Canon) on Feb 06, 2007 at 23:31 UTC
    When you add each file's lines to @content you are copying the entire array every time around the loop. This could quickly get expensive. It would probably be better to push onto the array, so instead of

    @content= (@content, <FH>);

    you could do

    push @content, <FH>;

    I ran a benchmark of these two methods on a sample of 65 scripts totalling 2082 lines, "Copy" being your original and "Push" my suggested alternative. Here's the benchmark code

    and here's the result

    Rate Copy Push Copy 2.32/s -- -94% Push 41.9/s 1710% --

    As you can see, copying the array each time takes a lot of resources.

    Cheers,

    JohnGG