[tpm] Replace a string with the result of shell command

Mike Stok mike at stok.ca
Sat Jan 9 10:28:23 PST 2010


On Jan 9, 2010, at 12:52 PM, Antonio Sun wrote:

> Hi, 
> 
> Here is the simplified example of what I want to do:
> 
>   DB<1> $v = 'AAabcDDabc'
>   DB<2> $v = m/abc/`echo $& | rev`/g
> Backticks found where operator expected . . . 
> 
> Please don't take it literally, and argue that shell invoking is not necessary -- I want to replace a certain string with the result of shell command, and pass the matched string to the shell command as well.

f you really want to do it that way then you can use s///ge for example:

  DB<1> sub rev_chomp { my $v = `echo $_[0] | rev`; chomp $v; $v }

  DB<2> x rev_chomp 'foo'
0  'oof'
  DB<3> $v = 'AAabcDDabc'

  DB<4> $v =~ s/(abc)/rev_chomp($1)/ge

  DB<5> x $v
0  'AAcbaDDcba'

Hope this helps,

Mike

-- 

Mike Stok <mike at stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20100109/14b0404e/attachment.html>


More information about the toronto-pm mailing list