Strawberry Perl - Search and replace in Windows

2019-08-22 00:53发布

问题:

I am trying to execute the following command:

perl -pi -e 's,vaadin-element,color-picker,g' *.* demo/* test/* src/* theme/*/*
(following this document)

Unfortunately it seems that the window distribution of pearl I use has some issues with the command, as I get the following error:

Can't open *.*: Invalid argument.
Can't open demo/*: Invalid argument.
Can't open test/*: Invalid argument.
Can't open src/*: Invalid argument.
Can't open theme/*/*: Invalid argument.

Any suggestions on how to fix that? Thank you in advance!

Disclaimer: I never used pearl before and have absolutely no experience.

回答1:

In unix systems, the shell expands globs and passes the file names to the program.

$ perl -e'CORE::say for @ARGV' *
file1
file2

The Windows shell, on the other hand, passes the values as is, and leaves it up to the program to treat them as globs if so desired.

>perl -e"CORE::say for @ARGV" *
*

You can perform the globbing as follows:

>perl -MFile::DosGlob=glob -e"BEGIN { @ARGV = map glob, @ARGV } CORE::say for @ARGV" *
file1
file2

The BEGIN block isn't generally needed, but it will ensure the globing once and early enough when using -n (which is implied by -p).

The -MFile::DosGlob=glob makes glob have Windows-like semantics. For example, it causes *.* to match all files, even if they don't contain ..

Integrated:

perl -i -MFile::DosGlob=glob -pe"BEGIN { @ARGV = map glob, @ARGV } s,vaadin-element,color-picker,g" *.* demo/* test/* src/* theme/*/*


回答2:

In Unix-derived operating systems, the shell expands globs like *.*, and provides the command line as an array of strings to the program.

In Windows, the command line is a single string, and it is up to the program to interpret what it means, including things like quote characters and globs. If the program is a normal C program, the C runtime interprets command line, and expands the globs, and passes an array of strings to main. This is because the C standard requires this.

However Perl is not C. Use the File::Glob library to expand the arguments.