in reply to Process ID of qx

Let's not forget that the process no longer exists after the qx completes. But anyway, you could do this using open instead of qx, e.g.: (updated to correct pipe direction)
use POSIX ":sys_wait_h"; # e.g.: my ($pid, $listing) = pidqx( "ls" ); sub pidqx { # returns pid + the usual backtick result my $pid = open my $ph, "$_[0] |" or die "$!: $_[0]"; local $/ = undef(); my $backtick = <$ph>; close $ph; waitpid $pid, 0; return ( $pid, $backtick ); }

-M

Free your mind

Replies are listed 'Best First'.
Re^2: Process ID of qx
by kyle (Abbot) on Mar 05, 2007 at 14:16 UTC

    my $pid = open my $ph, "| $_[0]" or die "$!: $_[0]";

    Your open is opening for write instead of read. The command's output goes to STDOUT instead of to your variable. Try this:

    sub pidqx { # returns pid + the usual backtick result my ( $cmd ) = @_; my $pid = open my $ph, '-|', $cmd or die "$!: $cmd"; local $/ = undef(); my $backtick = <$ph>; close $ph; return ( $pid, $backtick ); }

    Also, you don't have to wait for the command to finish because close does that for you.