http://www.perlmonks.org?node_id=1000904


in reply to Using wildcards to open files.

G'day starface245,

That's not really enough information to provide a definitive answer.

Do you get that error once or multiple times? Is script.pl in the same directory as the *.msg files or accessible via $PATH? Have you tried anything to track down why the file(s) can't be found? Is the error being emitted by the code you posted or script.pl? Can you show us the code in script.pl?

You could try something like the following (redirecting the output to a temporary file and searching for No such file or directory).

$ perl -e ' use strict; use warnings; my @files = glob("*.msg"); for my $file (@files) { my $cmd = "ls -l $file"; system($cmd); } ' -rw-r--r-- 1 ken staff 10 26 Oct 03:01 a.msg -rw-r--r-- 1 ken staff 10 26 Oct 03:01 b.msg -rw-r--r-- 1 ken staff 10 26 Oct 03:02 c.msg

With a script.pl like this:

$ cat script.pl while (<>) { print "$_\n"; }

This code works fine for me:

$ perl -e ' use strict; use warnings; my @files = glob("*.msg"); for my $file (@files) { my $cmd = "perl script.pl < $file"; system($cmd); } ' Message A Message B Message C

But I also get the same output with a single line:

$ perl script.pl *.msg Message A Message B Message C

Consider whether you really need thousands of shell and Perl processes if a single Perl process would do the same job.

-- Ken

Replies are listed 'Best First'.
Re^2: Using wildcards to open files.
by starface245 (Novice) on Oct 25, 2012 at 18:34 UTC
    Your code works for me, how do I escape with filenames that has spaces and special characters? such as "A_name sub.msg"

      As aitap said above, quotemeta will do that, a la my $escaped  = quotemeta($file); or even $cmd = "perl script.pl < \Q$file\E";


      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        Both methods doesn't seem to work. It not escaping the space correctly. It doing "A_name\"