I think you are right: It's silly to execute this shell pipeline in a perl script.
And the way you do it is even worse.
If you want to put a complex shell command in a script, use a shell script, that's what it was made for.
If you just want to execute a shell command in a perl script, use the system command. Unless you need the output of the command, then use backticks.
Here is an example of a shell script doing the same as your perl script, but with much less resources:
#!/bin/sh
if [ $# -lt 3 ]; then
echo "
usage:
replace_in_tree <to_be_replaced> <replacement> '<filematcher>'
replaces the string "to_be_replaced" with "replacement"
from the current directory downwards the filetree
in files matching filematcher, e.g. '*' or '*.html'
example:
replace_in_tree 'Author: Bill Gates' 'Author: Larry Wall' '*.html'
"
exit
fi
find . -name "$3" -type f | xargs -n 255 perl -pi.bak -e 's/\Q$1\E/$2/
+g'
Update: I forgot to mention that your script will probably fail.
The third parameter (*.html) will be expanded by the shell, so perl will have the first filename in $ARGV
2 and not the glob.
Therefore I changed the usage to have the third parameter quoted.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.