I am calling the following script from my main Perl program. The script takes a process name and returns its PID.
The script is included in my main perl code by using the require key word:
require "getPid.pl";
and called using:
&pidGetter($processName);
getPid.pl is:
#!/usr/bin/perl -w
use strict;
use warnings;
use Proc::ProcessTable;
pidGetter($ARGV[0]);
sub pidGetter
{
my $ret="PROCESS ID NOT FOUND\n";
my $t = new Proc::ProcessTable;
my $procName = $_[0];
foreach my $p (@{$t->table})
{
if ($p->fname =~ /$procName/)
{
$ret = $p->pid;
}
}
return $ret;
}
However, when the script is called, I get the following warning:
Use of uninitialized value $procName in regexp compilation at getPid.pl line 19
The rest of the script seems to function fine.
It is my understanding that $procName is initialized by $procName = &_[0];
I have put in print statements to debug, and $procName does return a value, so it is initialised. Does anyone know why I am getting these warnings?
require "getPid.pl";evaluates the code contained ingetPid.pl. So you actually call thepidGetter()function twice: in therequire‘d script and in the main script. As$ARGV[0]isundefinside therequire‘d script, you get the warning.