Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: using wildcard character * in perlscript command line

by Tanktalus (Canon)
on Jan 28, 2005 at 15:18 UTC ( #425983=note: print w/ replies, xml ) Need Help??


in reply to using wildcard character * in perlscript command line

foreach my $arg (@ARGV) { foreach (glob $arg) { print; } }

The expansion you're seeing is due to the unix shell expanding wildcards prior to giving them over to the command being run, while Windows thinks that each program should have to do that itself, duplicating the same code all over your system.

Update:Taking BrowserUk's solution and combining it comes up with yet another WTDI... which, ashamedly, I should have done in the first place, since I use map for this type of thing all over the place in my own code.

foeach (map { glob } @ARGV) { print; }

(I prefer the block form of map, but you can do with that as you will.)


Comment on Re: using wildcard character * in perlscript command line
Select or Download Code
OT: Advantage of not expanding wildcard in the shell
by ikegami (Pope) on Jan 28, 2005 at 15:27 UTC

    There is an advantage to shell not expanding wildcards. It allows for doing: copy *.txt *.bak. To do the same in a shell which expand wildcards requires a loop in the shell, making it harder to run simple commands AND duplicating the same code all over your system.

    I wouldn't mind being able to do copy *.txt 's/\.txt$/.bak/'.

      Another sideeffect is that you don't end up with needing xargs or find -exec and all the similar solutions to work around wildcards that produce huge lists.

      The app. can choose--(IMO) should always choose--to use an iterator approach rather than a generator.


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.

        Of course that also means the overwhelming majority of commands on Windows only take a single file as parameter and do not expand wildcards, because the programmer didn't know how or couldn't be bothered to go through the trouble (considering that FindFirst/FindNext API, I can sympathize). So you end up with a DEL command you have to write a loop around if you want to delete more than one file. Even when coammds do take wildcards, you often end up having to write loops: there is no way to write the equivalent of cp *.pl *.pm *.html projects/foowebapp/ because COPY expects exactly one source argument, whether it's a wildcard or not.

        At least there are replacement shells like 4DOS which fix the builtins.

        Makeshifts last the longest.

      You just have to quote the arguments, and they won't be expanded:

      move "*.txt" "*.html"

      Of course the move command displayed here is just an example, which you'd have to code yourself...

      man 1 rename?

      For more complex cases there's the rename script distributed with Perl…

      Makeshifts last the longest.

      look for 'rename.pl' by Larry Wall. Does exactly what you want.

      Yes, I like that great feature in dos. It even works in norton commander: you select multiple files, press F6 (rename), and type a wildcard name such as ?????_0.* to rename all selected files.

Re^2: using wildcard character * in perlscript command line
by dorward (Curate) on Jan 28, 2005 at 16:38 UTC

    The expansion you're seeing is due to the unix shell expanding wildcards prior to giving them over to the command being run

    However, you don't have to use cmd as your shell under Windows. You can install bash or another shell that comes from UNIX. Then I would be very surprised if you couldn't enjoy nice wildcard expansion under that OS.

Re^2: using wildcard character * in perlscript command line
by blazar (Canon) on Jan 29, 2005 at 08:34 UTC
    foreach my $arg (@ARGV) { foreach (glob $arg) { print; } }
    (Nah!) Since wildcard expansion "should" happen before the args are passed to the script, I like to do it all at once as early as possible:
    BEGIN {@ARGV=map glob, @ARGV}
    And then the rest of code can plainly ignore what happened with no need of ad hoc interventions...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2014-10-30 13:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls