Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Using wildcards to open files.

by kennethk (Monsignor)
on Oct 25, 2012 at 15:48 UTC ( #1000891=note: print w/ replies, xml ) Need Help??


in reply to Using wildcards to open files.

Do your file names have characters that need escaping or quoting, e.g. $cmd = "perl script.pl < '$file'";? Have you actually tested that the code as posted fails in your context? It works for me for some simple test systems.

The simplest solution from my perspective would be to avoid shelling and modify script.pl to process multiple files. That will give you access to the actual diagnostics and remove a layer of misdirection.

Update: If you can't modify script.pl for whatever reason, you can handle the file opens and piping yourself with something like:

#!/usr/bin/perl -w use strict; my @files = glob("*.msg"); foreach my $file(@files) { open my $in, '<', $file or die "Open fail on $file: $!\n"; my @lines = <$in>; open my $out, '|-', 'perl script.pl' or die "Piping fail: $!\n"; print $out @lines; }
Note that making those variables lexical to the loop (my) is significant, and you will likely need to explicitly close the filehandles otherwise. See |- in open or Safe Pipe Opens for some background.

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


Comment on Re: Using wildcards to open files.
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000891]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2014-04-21 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls