简体   繁体   English

在 Perl 中的替换 (s///) 错误中使用未初始化的值

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

I'm not used to Perl but had to create this function.我不习惯 Perl 但不得不创建此函数。

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/,/./;错误指出$rs =~ s/,/./; line.线。

Is there any error in the way I'm replacing it??我替换它的方式有什么错误吗?

The $url value is valid. $url 值有效。

If $rs is undefined, then it must be because the match " $rs =~ m,([0-9;]+) +EUR!; " failed, leaving $1 undefined.如果 $rs 未定义,那一定是因为匹配“ $rs =~ m,([0-9;]+) +EUR!; ”失败,导致$1未定义。 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.然后sendRequest失败,导致$rs未定义。

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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM