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


Hello,
I am trying to read in a list of processes using open, but the syntax; which works on the commandline, does not work in my perl script.
Would appreciate some ideas on what's causing the errors below.
Thanks!
open (IN, "ps -aux | awk -F" " '{print $1,$2 }' |" ) || die("Can't op +en IN"); String found where operator expected at prog1.pl line 71, near ""ps -a +ux | awk -F" " '{print $1,$2}' |"" (Missing operator before " '{print $1,$2}' |"?)

Replies are listed 'Best First'.
Re: reading unix command with open syntax error
by chromatic (Archbishop) on Mar 17, 2009 at 03:51 UTC

    That error message means that you have a string (specifically " '{print $1,$2 }' |") where a string shouldn't be. While you intended to write only one string for the piped open, you have two strings, thanks to the field separator passed to awk. This is a good use of an alternate quoting construct:

    open my $IN, q!ps -aux | awk -F" " '{print $1,$2 }' |! or die "Can't open pipe";

    I switched to the non-interpolating form because you probably don't want to interpolate the regexp match variables $1 and $2 into your string there.

    Of course, you could skip the pipe to awk and process the result with Perl (split) will come in handy....

      Thanks!

      You're right... It was easier to just let perl handle it in the split. Although I could not get the q! to work for some reason.. but would like to better understand how this works.. can you point me to some FAQs or documents?

      Thanks again!

        See "Quote and Quote-like Operators" in perlop.

Re: reading unix command with open syntax error
by codeacrobat (Chaplain) on Mar 17, 2009 at 23:49 UTC
    Why using the ugly awk-pipe in the first place?
    perl -e ' for (`ps aux`){ ($pid,$ppid) = (split/\s+/)[1,2]; print "$pid $ppid\n"} '

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});