Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Perl on windows, one-liners with wildcards/glob

by Yary (Pilgrim)
on Oct 21, 2011 at 23:17 UTC ( [id://932992]=perlquestion: print w/replies, xml ) Need Help??

Yary has asked for the wisdom of the Perl Monks concerning the following question:

Time to time I find myself on a windows host trying to run a one-liner such as:
perl -pi.bak "s/vanilla/strawberry/gi" *.txt *.doc
And then Window's cmd shell passes along *.txt and *.doc to perl, and perl cannot find a file called *.txt or *.doc, only files like "flavors.txt" and "plants.doc".

In general I can't install modules on these systems, and don't have friendlier shells to choose from. So I will either copy & paste the full files names, or if there are too many, I'll add a BEGIN block to the one-liner, eg:

perl -pi.bak "BEGIN{@ARGV=map glob,@ARGV}s/vanilla/strawberry/gi" *.txt *.doc

Anyone want to golf? Less to type for the one liners, the better.

/me wishes there was a command-line switch to have the perl interpreter do the globbing for me.

Replies are listed 'Best First'.
Re: Perl on windows, one-liners with wildcards/glob
by Corion (Patriarch) on Oct 21, 2011 at 23:39 UTC

    Have you looked at Win32::Autoglob?

    As a caveat, both, your solution and Win32::Autoglob use glob, which commonly breaks on directories with spaces in them unless special care is taken. Personally, I prefer the saner bsd_glob from File::Glob.

Re: Perl on windows, one-liners with wildcards/glob
by BrowserUk (Patriarch) on Oct 21, 2011 at 23:55 UTC

    I tend to use:

    dir /b | perl -ne ...

    because it gives me access to all the selection, exclusion, and directory traversal mechanisms that command has to offer.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Perl on windows, one-liners with wildcards/glob
by runrig (Abbot) on Oct 21, 2011 at 23:34 UTC
    If you could install MSYS (or perl power tools), you could do:
    dir /x /b *.txt *.doc | xargs perl -pi.bak -e "s/vanilla/strawberry/gi +"
    (update: which breaks on files with spaces unless you use the /x)
Re: Perl on windows, one-liners with wildcards/glob
by graff (Chancellor) on Oct 22, 2011 at 02:00 UTC
    Last time I checked (quite a while ago, admittedly), the GNU "bash" shell is available for MS-Windows systems. I highly recommend that.
Re: Perl on windows, one-liners with wildcards/glob
by mrstlee (Beadle) on Oct 22, 2011 at 16:08 UTC

    Is Cygwin an option for you?
    You get a decent unix environment including bash and a few other shells. Perl is also available. You can of course use ActiveState or Strawberry from a cygwin shell - you just have to be mindful of the different path formats between windows & cygwin.
    I've used it for years - it helps me forget I have to use windows
    There's a full X environment available - so you can have xterm, though personally I use the non-X rxvt terminal - better performance. Recently I've started using cygputty - a putty port which is working out just fine for me.

Re: Perl on windows, one-liners with wildcards/glob
by Anonymous Monk on Oct 22, 2011 at 06:04 UTC
    for %f in (*.c *.pl) do perl -pi.bak "s/vanilla/strawberry/gi" %f
Re: Perl on windows, one-liners with wildcards/glob
by Anonymous Monk on Oct 23, 2011 at 05:56 UTC
    perl -pi.bak -e "INIT{@ARGV=<@ARGV>}s/vanilla/strawberry/gi" *.txt *.d +oc
      Finally a golf answer, and I learned something from it. I knew of <*.txt> but had no idea that it allowed for multiple globs at once, such as <*.txt *.doc>.

      --
      [ e d @ h a l l e y . c c ]

      Is there a reason to use INIT instead of BEGIN except to play golf?

Re: Perl on windows, one-liners with wildcards/glob
by Yary (Pilgrim) on Oct 24, 2011 at 18:30 UTC
    Thanks to the various replies. I particularly like the INIT{@ARGV=<@ARGV>} one, and the one reminding about spaces in paths- they dovetail nicely. "for %f in ..." answer is a good one to remember as well.

    These particular hosts are somewhat clamped down, so while I do use cygwin on my development machine, and install CPAN modules, I can't on those hosts, nor can I easily share folders/remote mount drives between my dev environment and theirs.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://932992]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-04-24 00:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found