Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am a complete perl newbie: I have inherited some ksh script with embedded perl and would like some syntax help. here is what I have recieved and added:
if [ "X${substitute}" = "Xy" -o "X${substitute}" = "XY" ] then cat $inputfile | perl -e 'while(<STDIN>) { if( length( $_ ) > 3 ) { @args=split(/\|/,$_); if( $args[1] eq '$service' && $args[3] eq '$bundle' ) { @args=split(/\|/,$_); $args[4] =~ s/N/Y/; print join("|",@args); } else { print $_ } } else { print $_ } }' > $tmpfile

The gist of the code is to modify a line in a file by counting out args seperated by a |.

TIA,

tm

Replies are listed 'Best First'.
Re: Syntax question...
by Fletch (Bishop) on Apr 17, 2002 at 16:24 UTC

    And your question is . . . ?

    That obvious lack not withstanding:

    • Useless use of cat; pass the filename to be grobbled as an argument
    • Use -p, and possibly -i given the fact that it's outputting to a temporary file probably with the intention of replacing $inputfile with the munged version. See perldoc perlrun
Re: Syntax question...
by Juerd (Abbot) on Apr 17, 2002 at 16:22 UTC

    And the question is...? We can't help you unless you specify a question :)

    Here is a rewrite, by the way: [untested]

    perl -pe' if (length > 3) { my @args = split /\|/; # I'm surprised the following line works in a # shell script, with the single quotes and all... if ($args[1] eq '$service' and $args[3] eq '$bundle') { $args[4] =~ s/N/Y/; $_ = join "|", @args; } } ' $inputfile
    There, with proper indenting and an implicit loop and use of implicit handling of $_, it looks a lot better. If you add -i, the file itself will be changed, which may remove the need for a temporary file.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

Re: Syntax question...
by jepri (Parson) on Apr 17, 2002 at 16:21 UTC
    You don't seem to define $service or $bundle anywhere. If you defined them in a ksh script, or as environment variables, perl won't automatically pick them up. You will have to arrange for perl to get at them somehow. IIRC, %ENV holds environment variables, so that's probably the easiest way combined with shell scripts.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: Syntax question...
by erikharrison (Deacon) on Apr 17, 2002 at 16:24 UTC

    This seems to do what you want. A single advisement - $service and $bundle both look like Perl scalars - these scalars will not interpolate into a single quoted string, as you have them. As these (possible) variables are also not defined in your script, then I think that the single quotes are probably intentional - but you might want to make sure.

    Also, I don't know a thing about ksh, but I am pretty sure that with this ratio of embedded Perl to ksh, then this might profitably be rewritten as a pure Perl program, also helping you learn the language.

    Cheers,
    Erik