开发者

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

开发者 https://www.devze.com 2023-04-05 08:08 出处:网络
It would be nice for Perl scripts on Windows to work like they do on Linux with regard to wildcard containing file names.

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 scr开发者_高级运维ipt 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
...


  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.


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.


Simple answer:

my @args = glob "@ARGV";


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

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

精彩评论

暂无评论...
验证码 换一张
取 消