Problem with Perl warnings and regex Use of uninitialized value in regexp compilation
I am calling the following script from my main Perl program开发者_JAVA技巧. 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 in getPid.pl
. So you actually call the pidGetter()
function twice: in the require
'd script and in the main script. As $ARGV[0]
is undef
inside the require
'd script, you get the warning.
精彩评论