Search code examples
windowsperlreplacestrawberry-perl

Strawberry Perl - Search and replace in Windows


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.


Solution

  • 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/*/*