how to pass scripts to perl -pe

Discussion in 'Perl Misc' started by Daniel Kramer, Feb 26, 2004.

  1. 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
     
    Daniel Kramer, Feb 26, 2004
    #1
    1. Advertising

  2. Daniel Kramer wrote:
    >
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Feb 26, 2004
    #2
    1. Advertising

  3. Daniel Kramer

    gnari Guest

    "Daniel Kramer" <> wrote in message
    news:...
    > 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
     
    gnari, Feb 26, 2004
    #3
  4. "John W. Krahn" <> wrote in message news:<>...

    >
    >
    > 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
     
    Daniel Kramer, Feb 26, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Danielsson
    Replies:
    8
    Views:
    645
    Mike Meyer
    Jul 22, 2005
  2. Jp Calderone
    Replies:
    0
    Views:
    467
    Jp Calderone
    Jul 21, 2005
  3. davidj411
    Replies:
    0
    Views:
    553
    davidj411
    Jun 27, 2008
  4. J Krugman
    Replies:
    7
    Views:
    163
    Ben Morrow
    Jun 14, 2004
  5. Replies:
    13
    Views:
    554
    Anno Siegel
    Sep 10, 2007
Loading...

Share This Page