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

Hi
I'm fairly new to Perl and am struggling with what is probably a simple solution to a problem.
I'm using Proc::ProcessTable to get a list of all currently running pids on a system.
I need to traverse this array to ascertain whether a certain pid is present or not.
$process = new Proc::ProcessTable; @fpid = "1234";
To get access to the pid I would use $process->pid. The question is what is the best way to see whether this pid is present or not ? Should I use a grep or exists or some other method ?. My knowledge of Perl is at the novice stage hence my question.

Replies are listed 'Best First'.
Re: grep or exists
by blazar (Canon) on May 16, 2006 at 12:01 UTC

    (untested)

    #!/usr/bin/perl use strict; use warnings; use Proc::ProcessTable; my $p=Proc::ProcessTable->new; my $wanted=1234; for ( @{$p->table} ) { print "Found\n" and last if $_->pid == $wanted; } __END__
    To get access to the pid I would use $process->pid.

    No, you would use the pid() method on the objects of $process->table() which are instances of the class Proc::ProcessTable::Process.

Re: grep or exists
by zentara (Cardinal) on May 16, 2006 at 13:05 UTC
    You might find this interesting to look at
    #!/usr/bin/perl use Proc::ProcessTable; $FORMAT = "%-6s %-10s %-8s %-24s %s\n"; $t = new Proc::ProcessTable; printf($FORMAT, "PID", "TTY", "STAT", "START", "COMMAND"); foreach $p ( @{$t->table} ){ printf($FORMAT, $p->pid, $p->ttydev, $p->state, scalar(localtime($p->start)), $p->cmndline); } ############################################################# # Dump all the information in the current process table use Proc::ProcessTable; $t = new Proc::ProcessTable; foreach $p (@{$t->table}) { print "--------------------------------\n"; foreach $f ($t->fields){ print $f, ": ", $p->{$f}, "\n"; } }

    I'm not really a human, but I play one on earth. flash japh
Re: grep or exists
by jesuashok (Curate) on May 16, 2006 at 12:21 UTC
    hi

    (untested)

    #!/usr/bin/perl use strict; use warnings; use Proc::ProcessTable; my $p=Proc::ProcessTable->new; my $wanted=1234; print "Found\n" if grep(/^$wanted$/,@{$p->table} );

    "Keep pouring your ideas"
      grep(/^$wanted$/,@{$p->table} )
      There is abolutely no need to limit use of grep in Perl to regular expressions. Any kind of boolean expression, or expression used as a boolean, for the filter condition, will do. For example:
      grep($_ == $wanted, @{$p->table} )
      update: Better, as blazar points out, code that actually works for this case:
      grep($_->pid == $wanted, @{$p->table} )

      That way it won't work:

      $ perl -MProc::ProcessTable -le 'print for @{Proc::ProcessTable->new-> +table}' Proc::ProcessTable::Process=HASH(0x814cb3c) Proc::ProcessTable::Process=HASH(0x827d44c) Proc::ProcessTable::Process=HASH(0x8276e84) Proc::ProcessTable::Process=HASH(0x827d590) Proc::ProcessTable::Process=HASH(0x8276f74) Proc::ProcessTable::Process=HASH(0x8277890) Proc::ProcessTable::Process=HASH(0x8277a40) Proc::ProcessTable::Process=HASH(0x8277bf0) ...

      Your match has to become

      $_->pid =~ /^$wanted$/

      But even then it would be an abuse of grep and of a pattern match, IMHO. Both slightly suboptimal, for

      • the implicit grep cycle will continue also after the pid has been matched;
      • the match is either less efficient than a comparison or optimized to a comparison, I can't remember which is which. In any case why not going for an explicit comparison to begin with?
      A question I have about both of these answers is what happens if the value being searched for i.e. $wanted appears in a field other than the pid field ?. If I understand the suggested code correctly the whole process table (including cpu,ctime,gid etc.. ) is being searched . Hence if the value appears in cpu instead of pid then it would still flag as having found the pid . Perhaps you could comment ?

        The post you're replying to contains code that -as such- won't work. With a simple correction it would become equivalent to mine. In that case neither of them would claim having found the pid if it's not in the pid field. That's why the method is called... ehm... pid().