Skip to main content

Perl, Wildcards and the Windows Command Line

·1 min

When developing for multiple platforms, its always the little things that catch you out.

Heres one example I found while developing gcal, a Command Line Interface to Google Calendar. On Unix (including Mac OS X shells) wildcard expansion is done by the shell, and then the expanded list of files is passed on to the program being run. On Windows, the shell doesn’t do any expansion, and it’s up to programs to do the expansion1.

To get around this in Perl, you can do something like this2.

my @args = ($^O eq 'MSWin32') ? map { glob } @ARGV : @ARGV;

Here we are passing all the arguments to glob, which will do the shell expansions, including wildcards. We only want to do this for Windows, as if you use it on Unix you risk globbing real file names - asterisks and question marks are valid characters in Unix file names.


  1. The difference in wildcard expansion between Windows and unix/Macintosh ↩︎

  2. How can I handle wildcards on the command line using Perl on Windows? - Stack Overflow ↩︎


Want great, practical advice on implementing data mesh, data products and data contracts?

In my weekly newsletter I share with you an original post and links to what's new and cool in the world of data mesh, data products, and data contracts.

I also include a little pun, because why not? 😅

(Don’t worry—I hate spam, too, and I’ll NEVER share your email address with anyone!)


Andrew Jones
Author
Andrew Jones
I build data platforms that reduce risk and drive revenue.