you're already doing that! the backticks (``) capture the output of sed, and place it into $haha. (You're missing a dollar sign at the front of the line.) print($haha) and you'll see the fixed up text :)
$haha = 'I have black tyres.';
$haha=`echo $haha | sed "s/tyre/tire/g"`;
print($haha, "\n");
# outputs: I have black tires.
However, there's no need to use sed in this fashion. perl is quite good at doing text manipulations, including regular expressions:
$haha = 'I have black tyres.';
$haha =~ s/tyre/tire/g;
print($haha, "\n");
# outputs: I have black tires.
| [reply] [d/l] [select] |
It's generally considered "bad form" to call sed or awk from perl, as Perl for the most part supercedes both of them. For instance, your example could be expressed as:
$haha =~ s/tyre/tire/g;
Also, as part of the standard perl distribution, there'a a2p and s2p, the awk-to-perl and sed-to-perl translators, respectively.
Welcome to perl, thor
Feel the white light, the light within
Be your own disciple, fan the sparks of will
With all of us waiting, your kingdom will come
| [reply] [d/l] |
Almost exactly the same way you did it with sed, but you need to add a few switches:
haha=`echo $haha | perl -pe "s/tyre/tire/g"`
"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.
| [reply] [d/l] |
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
| [reply] [d/l] [select] |