how to pass scripts to perl -pe

D

Daniel Kramer

what are the formatting rules to passing perl commands to the perl
command line? I've tried the following, which works:


echo testme | perl -pe '@test = split /t/, $_ ; $_= $test[2]'


I'm actually trying to shell out of another scripting lang that's not
very good at string parsing to have perl do some work.. the only
problem is this other lang doesn't like the ";" in my perl command
string and fails..I'm basically passthing this cmd though a "system"
like function in the other lang and it sees the ";" as an end of line,
not part of the string to pass. Is there another notation I can use
on a single
line to end commands instead of the ";"?

any suggestions?

thanks

daniel
 
J

John W. Krahn

Daniel said:
what are the formatting rules to passing perl commands to the perl
command line? I've tried the following, which works:

echo testme | perl -pe '@test = split /t/, $_ ; $_= $test[2]'

I'm actually trying to shell out of another scripting lang that's not
very good at string parsing to have perl do some work.. the only
problem is this other lang doesn't like the ";" in my perl command
string and fails..I'm basically passthing this cmd though a "system"
like function in the other lang and it sees the ";" as an end of line,
not part of the string to pass. Is there another notation I can use
on a single
line to end commands instead of the ";"?

any suggestions?


In your example above you don't really need a semicolon.

echo testme | perl -pe'$_ = ( split /t/ )[2]'


However, to answer your question, you can enclose each statement in
braces.

echo testme | perl -pe'{@test = split /t/} {$_= $test[2]}'


Or you could use the logical 'and' operator.

echo testme | perl -pe'@test = split /t/ and $_= $test[2]'



John
 
G

gnari

Daniel Kramer said:
what are the formatting rules to passing perl commands to the perl
command line? I've tried the following, which works:


echo testme | perl -pe '@test = split /t/, $_ ; $_= $test[2]'


I'm actually trying to shell out of another scripting lang that's not
very good at string parsing to have perl do some work.. the only
problem is this other lang doesn't like the ";" in my perl command
string and fails..I'm basically passthing this cmd though a "system"
like function in the other lang and it sees the ";" as an end of line,
not part of the string to pass. Is there another notation I can use
on a single
line to end commands instead of the ";"?

well sometimes it is possible to rearrange the perl into
one statement using things like '&&' and 'and'
perl -pe '@test = split /t/, $_ and $_= $test[2]'

or by using a match instead of split
perl -pe '($_)=/.*?t.*?t(.*?)t/'

but maybe your language has some support for escaping the ;
or maybe you need to quote the whole command line

another way might be to write the script to a temp file and do
echo testme | perl -p tmpfile.pl


gnari
 
D

Daniel Kramer

John W. Krahn said:
In your example above you don't really need a semicolon.

echo testme | perl -pe'$_ = ( split /t/ )[2]'


However, to answer your question, you can enclose each statement in
braces.

echo testme | perl -pe'{@test = split /t/} {$_= $test[2]}'


Or you could use the logical 'and' operator.

echo testme | perl -pe'@test = split /t/ and $_= $test[2]'



John

Ah thanks very much for the great example and info. Works like a charm

-daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top