I have often needed to use Perl in an MS Windows context. One real problem with is the MS shells (non)handling of wildcards. For those that have not had the experience, the MS shells assume that the program will do globbing on wildcards. So if you call your script with '*.txt' as an argument, you get one argument. I have written several versions of this over the years. This is my current one. Hope it helps someone.
@ARGV = map { /[*?]/ ? glob $_ : ($_) } @ARGV;

Replies are listed 'Best First'.
Re: Fix wildcard arguments under MSWin
by gwadej (Chaplain) on Jul 27, 2003 at 23:21 UTC

    Oops. Forgot to log in before submitting.

    This code should be placed at the very top of the script. After which @ARGV will contain what you probably expected in the first place.

      Since there's no harm in globbing names that lack wildcards, you can skip that check. And since glob looks to $_ if missing explicitly passed parameters, you can shorten this all to
      @ARGV = map glob, @ARGV;

      Makeshifts last the longest.

        That's true.

        The test was actually there on purpose. I had scripts that I ran under MS and Unix. With the test I reduced the number of calls to glob when the command line was expanded properly. This made a significant difference in some of my code.

        I originally had an OS check in there, that I dropped when I started typing this into code. I then used the ternary test to correct for slow reactions on some Unix.

        Probably in the usual case this would be better. It's certainly easier to remember.

Re: Fix wildcard arguments under MSWin
by jacques (Priest) on Aug 12, 2003 at 01:44 UTC
    CPAN is always there for you like a good neighbor that cares.

    Win32::Autoglob

      Cool.

      Unfortunately, this module did not exist back when I first made this little piece of code. I've been using this quick one-liner for so long it just trips off my fingertips when I need it.

      Interestingly enough, that module also does a check for metacharacters before passing to glob. I believe others pointed out that I didn't need to do this.<grin/>

      G. Wade