That's not Perl's fault, it's Windows' fault. Linux shells expand the wildcards for you, cmd.exe does not. I don't know about PowerShell, but it seems it doesn't either.
Also, you don't want to interpret the asterisk the regex way, but the glob pattern way. In a regex, *
means "the previous thing should be repeated zero or more times".
You can use Perl's glob function:
perl -p -i'.bak' -e 'BEGIN {@ARGV = glob shift} s#__SUB__CWD__#'$(pwd)'#g' *.sql
Note that you need to backslash backslashes in the path to prevent Perl from interpreting them, i.e. use $($pwd -replace '\\', '\\')
instead of just $(pwd)
. You also need to backslash the #
characters in the path as they would otherwise be understood as the delimiters, so use $($pwd -replace '[\\#]', '\$&')
.
Therefore, it would be even cleaner not to use $(pwd)
, but let Perl know what the replacement is:
perl -p -i'.bak' -e 'BEGIN{ ($pwd, @ARGV) = (shift, map glob, @ARGV) }
s/__SUB__CWD__/$pwd/g' -- $(pwd) *.sql
The first argument is assigned to $pwd
via shift, the remaining attributes are mapped to glob, which means you can specify more than one wildcard pattern.