开发者

Use of uninitialized value in substitution (s///) error in Perl

开发者 https://www.devze.com 2023-02-09 12:39 出处:网络
I\'m not used to Perl but had to create this function. sub getPrice { my $url = shift; my $prdid = shift;

I'm not used to Perl but had to create this function.

sub getPrice {
   my $url = shift;
   my $prdid = shift;
   my $count = shift;
   my $totcount = shift;

   print "($count/$开发者_运维百科totcount) Fetching Product Price : $prdid .";    
   my $rs = sendRequest('GET', $url);
   print "url :".$url;
   print "..\n";

   $rs =~ s!.*Unit Price Excl. VAT!!s;
   $rs =~ s!</table>.*!!s;

   $rs =~ m!([0-9,]+) +EUR!;
   $rs = $1;
   $rs =~ s/,/./;

   return $rs;
}

When I call this function I get this error.

Use of uninitialized value in substitution (s///)

The error points out the $rs =~ s/,/./; line.

Is there any error in the way I'm replacing it??

The $url value is valid.


If $rs is undefined, then it must be because the match "$rs =~ m!([0-9,]+) +EUR!;" failed, leaving $1 undefined. Adding some strategic print statements should help.


If the error is on the line

$rs =~ s!.*Unit Price Excl. VAT!!s;

then sendRequest fails, leading to $rs being undefined.

The error could also be on line of the last substitution, meaning that the prior match failed. Wrap the match in a conditional statement to be sure.

if ( $rs =~ m!([0-9,]+) +EUR! ) {
    $rs = $1;
} else {
    die "no matching";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消