Re: using wildcard character * in perlscript command line
by Tanktalus (Canon) on Jan 28, 2005 at 15:18 UTC
|
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.) | [reply] [d/l] [select] |
|
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/'.
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
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.
| [reply] |
|
|
|
|
| [reply] |
|
look for 'rename.pl' by Larry Wall. Does exactly what you want.
| [reply] |
|
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.
| [reply] |
|
| [reply] |
|
BEGIN {@ARGV=map glob, @ARGV}
And then the rest of code can plainly ignore what happened with no need of ad hoc interventions...
| [reply] [d/l] [select] |
Re: using wildcard character * in perlscript command line
by BrowserUk (Patriarch) on Jan 28, 2005 at 15:19 UTC
|
perl -wle"BEGIN{ @ARGV = map glob, @ARGV } print for @ARGV" *
Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.
| [reply] [d/l] |
|
I looked at this and thought, "no way, what if the script gets switches or other arguments that are not files (like script * -option), those arguments will get lost because they donīt match any filename.
But they donīt. Can you explain why?
| [reply] |
|
Because glob() will simply pass-thru anything that doesn't need expansion, otherwise it would lose ordinary filenames. But if the 'option' might have the expandable characters '*' or '?' you will be in danger of losing _those_ options. glob('-f?') returns nothing (usually), though glob('-f') will simply return '-f'.
| [reply] |
|
P:\test>perl -wle" print for glob '-debug' "
-debug
As for the why, if there is no file of that name existing, you'll need someone with the historical perspective (merlyn?).
Maybe, just so that it doesn't affect non-wildcard parameters?
Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.
| [reply] [d/l] |
|
Perl's glob was originally implemented in terms of csh's. From man csh:
If a word contains any of the characters *, ?, [ or { or begins
with the character ~ it is a candidate for filename substitution,
also known as globbing.
If you use the bsdglob function from File::Glob (which is how glob is implemented nowadays), you can pick behaviour as desired.
Makeshifts last the longest.
| [reply] |
|
For the same reason for which (depending on an option, really) *NIX shells (well, at least bash that I know) pass through any unmatched item.
| [reply] |
Re: using wildcard character * in perlscript command line
by Overthruster (Initiate) on Jan 28, 2005 at 18:07 UTC
|
foreach $arg ( @ARGV ) {
@files = glob $arg;
foreach (@files) {
...
| [reply] [d/l] |
Re: using wildcard character * in perlscript command line
by Willworker (Acolyte) on Jan 28, 2005 at 19:20 UTC
|
I ran into this same problem. In an older version (<5.0) of perl, "perl script.pl *.ext" worked the way I expected it to, by expanding the * wildcard, but after upgrading to perl 5.8, this no longer worked, so I had to do something like the examples already given (doing the globbing/*-expansion with a couple lines of code).
Steve | [reply] |