Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
So, as others mention, what you should be doing is simply:
$haha =~ s/tyre/tire/g;
(that's it... simple, no backticks, no need for invoking a subshell and sed, since sed is kind of just a very very limitted subset of perls functionality).

However, I'll also give the direct answer to your question: Perl and shell have a great deal in common, for example the $ sigil on variables, the interpolation of variables inside of double quotes ("...") and backticks (`...`), and the (shell) execution of a string in backticks. The important differences (for this example) are that

  • in perl, you need to use the $ sigil on your variables, even when they are on the left-hand side of an assignment (unlinke in shell)
  • shell will automatically handle whitespace* from the results of something executed by backticks (which perl will not do... perl will just give you the output of the subshell exactly, so, in this case, including the trailing newline from the echo | sed)
So in order to get the specific behavior you want doing this shell programming in perl, you'd have to do:
$haha=`echo -n $haha | sed "/tyre/tire/g"`;
There are only two actual differences (corresponding to the two bullets above):
  • changed haha=... to $haha=...
  • adding a -n option to echo. This makes echo not include a trailing newline. If echo had included that newline, then sed, too, would have passed that trailing newline, and perl would have not trimmed it off, so you've had ended up (inadvertantly) adding a newline to the end of $haha.

NOTE Regarding the shell and it's "handling" of whitespace: in general, the shell is actually going to be doing a lot more than just trimming the trailing newline... what the shell does is basically split all the output on any whitespace (including spaces, tabs, newlines), ignore any leading or trailing whitespace. It will use these "words" as a list, if the backtick is used in a list context, and rejoin the split-apart "words" with single spaces if it is using the results of the backtick as a string.

------------ :Wq Not an editor command: Wq

In reply to Re: sed in perl by etcshadow
in thread sed in perl 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 drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-25 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found