On Dec 20, 2003, at 8:06 AM, Peter Scott wrote: [..]
You are missing a '|' in there.

thanks for the catch... [..]
No reason not to make that much simpler using backticks:

sub run_cmd
{
  my $cmd = shift;
  my $args = shift || '';
  my @output = `$cmd $args 2>&1`;
  $? < 0 and return "ERROR: problem running $cmd with $args: $!";
  [ grep /\S/, map { chomp; $_ } @output ];
}

The only reason I left $cmd and $args separate is so the error
message could be identical; I think it would be better off
combining them into a single parameter.

I like the grep/map trick there... Stylish.


I will confess to 'emotional issues' about 'back ticks'
since that is all one has in bourne shell, and that it
was easier for me to convert to the popen() style in perl.

Normally when I am doing such a thing it is because
I want to filter inside

while(<CMD>) BLOCK

eg something like
        ...
        my $selfPid = open(PS, "ps $me->{psArg} 2>&1|" ) ;
        $selfPid or die "no grovel ps data:$!\n";
        ....
        while(<PS>) {
        chomp;
        my ($pid, $ppid, $pidT) = $me->parse_cmd_line($_);
        next if $pid == $selfPid ; # skip our child
                ...
        if ( exists( $me->{psTree}->{ $ppid } )) {
            push(@{$me->{psTree}->{ $ppid }->{'children'}},$pid);
        } else {
            $me->{psTree}->{ $ppid } = $me->null_ppid_struct($pid);
        }
                $me->{psTree}->{ $pid } = $pidT;
                ...
        $me->{psTree}->{ $pid }->{children} = []
                unless ( exists( $me->{psTree}->{ $pid }->{children} ) );
    }

Which I have always found to be a much more useful
strategy in the long run than either the system()
or backtick, where so many of us start.

The first step into that of course is feeling at home
with the simple syntax and then off into deep water....

The longer term strategy of couse is to shift from
'calling other perl scripts' into the strategy of
making those 'external scripts' "subs in a module"
so that one totally saves on the 'forks' by keeping
all of the code internal at compile time...

ciao
drieux

---


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to