[tpm] Pipe expression result through external filter command

Shlomi Fish shlomif at shlomifish.org
Mon May 14 12:13:06 PDT 2012


Hi Antonio,

a few comments on your code.

On Mon, 14 May 2012 13:29:47 -0400
Antonio Sun <antoniosun at lavabit.com> wrote:

> On Mon, May 14, 2012 at 12:06 PM, Antonio Sun <antoniosun at lavabit.com>wrote:
> 
> > I need to calculate 12+34, then pipe this expression result through an
> > external shell filter command, say 'rev'.
> >
> > ultimately, I'm going to use the whole expression in the second part of
> > string replace 's/.../.../eg' operation.
> >
> 
> Maybe I have to do it in two steps:
> 
>  perl -e '$str = "1234"; $str =~ s/1234/$var = $&; `echo $var | rev`/e;
> print $str'
> 

1. Using $& will incur a slowdown on the rest of the matches done in the
program. You should instead do:

s/(1234)/$var = $1; [rest of expression here]/

2. Look into https://metacpan.org/module/IPC::System::Simple and
https://metacpan.org/release/IPC-Run instead of using backticks ( `...` ). 
If you are going to use backticks, make sure you quote the variables you
pass this way (which varies from shell to shell).

3. I hope the external command in question is harder to do properly in Perl
than the UNIX "rev" command. You may also wish to search CPAN for a module
doing something similar.

For more information, see:

http://perl-begin.org/tutorials/bad-elements/

Regards,

	Shlomi Fish 

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
List of Portability Libraries - http://shlom.in/port-libs

There is an IGLU Cabal, but its only purpose is to deny the existence of an
IGLU Cabal. 
    — Martha Greenberg

Please reply to list if it's a mailing list post - http://shlom.in/reply .


More information about the toronto-pm mailing list