Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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 $ARGV2 and not the glob.
Therefore I changed the usage to have the third parameter quoted.


In reply to Re: Re: RE: Replace across files (extreme newbie) by busunsl
in thread Replace across files (extreme newbie) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found