Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Using wildcards to open files.

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


in reply to Re^2: Using wildcards to open files.
in thread Using wildcards to open files.

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.


Comment on Re^3: Using wildcards to open files.
Select or Download Code
Re^4: Using wildcards to open files.
by starface245 (Novice) on Oct 25, 2012 at 20:06 UTC
    Both methods doesn't seem to work. It not escaping the space correctly. It doing "A_name\"

      Where's your code? Where's your input? Where's your output? A vague statement that it didn't work correctly is entirely inadequate. Please read How do I post a question effectively?.

      Both methods provided by kennethk work just fine!

      $ mv a.msg 'XYZ @ % a.msg' $ cat 'XYZ @ % a.msg' Message A $ cat script.pl print while (<>) $ perl -e ' use strict; use warnings; my @files = glob("*.msg"); for my $file (@files) { print "Filename: $file\n"; my $cmd = "perl script.pl < \Q$file\E"; system($cmd); } ' Filename: b.msg Message B Filename: c.msg Message C Filename: XYZ @ % a.msg Message A $ perl -e ' use strict; use warnings; my @files = glob("*.msg"); for my $file (@files) { print "Filename: $file\n"; my $escaped = quotemeta $file; my $cmd = "perl script.pl < $escaped"; system($cmd); } ' Filename: b.msg Message B Filename: c.msg Message C Filename: XYZ @ % a.msg Message A

      Also, my original suggestion of using one process instead of thousands still works without modification:

      $ ls -l *.msg -rw-r--r-- 1 ken staff 10 26 Oct 03:01 XYZ @ % 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 $ perl script.pl *.msg Message A Message B Message C

      -- Ken

Reaped: Re^4: Using wildcards to open files.
by NodeReaper (Curate) on Oct 25, 2012 at 20:07 UTC
      You would do well to read some of the literature on posting questions on help forums effectively. See, for example, I know what I mean. Why don't you?, How (Not) To Ask A Question, How do I post a question effectively? or How To Ask Questions The Smart Way.

      When you directly interpolate your name into your string, it's like you literally typed that on the command line. If file names contain whitespace or shell meta characters, then the literally-interpolated string will mean something other than you intend. The solution is to use techniques other than direct interpolation -- there are a number of ways to get Perl to do the heavy lifting for you. I posted one such solution in Re: Using wildcards to open files.. What doesn't work about it? Have you even tried it?

      I'm guessing that this is a Windows system since the solution posted would have worked otherwise. The Windows command line does not escape the same way that most systems do. In particular, it does not read "\ " as an in-name space. What you need to do for a MS system is wrap the name in double quotes, like $cmd = qq{perl script.pl < "$file"};


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-12-29 07:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (185 votes), past polls