Michael Goldshteyn
Michael Goldshteyn

Reputation: 74410

Making Perl on Windows work like Linux (wildcarded param expansion)

It would be nice for Perl scripts on Windows to work like they do on Linux with regard to wildcard containing file names.

For example:

perl myscript.pl *.txt

On Linux, bash will expand the *.txt to a set of file names and pass those to the perl interpreter as individual parameters. On Windows, the *.txt gets passed directly into Perl.

So, basically, what I am looking for is something to put at the top of the script that will expand wildcard params, so that the rest of the script can be the same as on Linux.

For example:

myscript.pl

use warnings;
use strict;

# Mystery code to expand all wildcard params, fudging ARGV in the process
# ----
<Insert code here>
# ----

# Rest of script
...

Upvotes: 3

Views: 760

Answers (4)

TLP
TLP

Reputation: 67900

Simple answer:

my @args = glob "@ARGV";

Upvotes: 2

brianary
brianary

Reputation: 9332

  1. Install Win32::Autoglob if your Perl doesn't come with it.
  2. Set the PERL5OPT environment variable to -MWin32::Autoglob or just use Win32::Autoglob.

Upvotes: 12

Joel
Joel

Reputation: 3483

Use this code snipet to expand the wildcard and get the file names:

my @list = <$ARGV[0]>;

Upvotes: 1

fvu
fvu

Reputation: 32973

There's a big difference between the shells of the operating systems that makes this less obvious: in most Unix shells, globbing (wildcard expansion) is done by the shell, on Windows this is not the case.

This means that an application started by a Unix shell will see a long list of filenames in argv, whereas the same application started under Windows will get 1 pattern with the wildcards embedded.

Starting the script in bash via Cygwin is possibly the cleanest option. You could also detect Windows ( How can I tell if my Perl script is running under Windows? ) and glob in Perl.

Upvotes: 4

Related Questions