在 Perl 中,将变量设置为正则表达式,我收到未初始化值警告
In Perl, set a variable to a regex, and I get an uninitialized value warning
所以我有这段代码,分析传递给 CGI 调用的参数值。
我有这段代码可以做到这一点。
if (defined($passed_args->{'between'})) {
my $between = $passed_args->{'between'};
my $date_field = @$between[0];
my $start_date = @$between[1];
my $end_date = @$between[2];
my $gdt_regex = /[0-9]{4}(-)((0[1-9])|(1[0-2]))(-)((0[1-9])|([1-2][0-9])|(3[0-1]))(\s)(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])/;
if (!defined($passed_args->{$date_field}) && $start_date =~ $gdt_regex && $end_date =~ $gdt_regex) {
$self->error('between parameter not populated correctly.');
return;
}
$query_url .= $date_field . "BETWEEN" . $start_date . "@" . $end_date . "^";
delete $passed_args->{'between'};
}
start_date 和 end_date 的值采用如下格式:
2019-04-04 00:00:00
在我的服务器日志中,我反复收到此警告:
Use of uninitialized value $_ in pattern match (m//) at /my/script.pm line 6316
6316行对应:
my $gdt_regex = /[0-9]{4}(-)((0[1-9])|(1[0-2]))(-)((0[1-9])|([1-2][0-9])|(3[0-1]))(\s)(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])/;
据我所知,您在使用未初始化的值进行模式匹配时会遇到该错误。但是 为什么 当我 将正则表达式分配给变量时我得到它? 捕获组是否有什么导致 Perl 不合适?还是我漏掉了什么?
据我所知,这不会造成任何伤害,但它经常出现在我的日志中,我想将其清除。
/.../
是匹配运算符。它检查模式是否与绑定变量匹配。如果没有显式绑定变量,它将与 $_
匹配,就好像您使用了 $_ =~ /.../
.
您正在寻找
my $gdt_regex = qr/.../;
好吧,我认为一些验证会对您的代码有益
注意:可能 $date_start
和 date_end
更适合与 $date_field
保持一致
use strict;
use warnings;
........
if (defined($passed_args->{'between'})) {
my($date_field,$date_start,$date_end) = @{$passed_args->{between}}[0..2];
{
$self->error('ERROR: between parameter not populated correctly.');
return;
} if ( !defined($passed_args->{$date_field})
&& !is_valid($date_start)
&& !is_valid($date_end)
)
$query_url .= $date_field . "BETWEEN" . $start_date . "@" . $end_date . "^";
delete $passed_args->{'between'};
}
#
# timestamp validation
#
sub is_valid {
my $timestamp = shift;
return 0 if $timestamp !~ /\d{4}-\d{2}-\d{} \d{2}:\d{2}:\d{2}/;
my($date,$time) = split ' ', $timestamp;
my($year,$month,$day) = split '-', $date;
my($hour,$min,$sec) = split ':', $time;
#return 0 if $year < 1900 or $year > 2019;
return 0 if $month < 1 or $month > 12;
return 0 if $day < 1 or $day > 31;
return 0 if $hour < 1 or $hour > 23;
return 0 if $min < 1 or $min > 59;
return 0 if $sec < 1 or $sec > 59;
return 1; # timestamp is valid
}
所以我有这段代码,分析传递给 CGI 调用的参数值。
我有这段代码可以做到这一点。
if (defined($passed_args->{'between'})) {
my $between = $passed_args->{'between'};
my $date_field = @$between[0];
my $start_date = @$between[1];
my $end_date = @$between[2];
my $gdt_regex = /[0-9]{4}(-)((0[1-9])|(1[0-2]))(-)((0[1-9])|([1-2][0-9])|(3[0-1]))(\s)(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])/;
if (!defined($passed_args->{$date_field}) && $start_date =~ $gdt_regex && $end_date =~ $gdt_regex) {
$self->error('between parameter not populated correctly.');
return;
}
$query_url .= $date_field . "BETWEEN" . $start_date . "@" . $end_date . "^";
delete $passed_args->{'between'};
}
start_date 和 end_date 的值采用如下格式:
2019-04-04 00:00:00
在我的服务器日志中,我反复收到此警告:
Use of uninitialized value $_ in pattern match (m//) at /my/script.pm line 6316
6316行对应:
my $gdt_regex = /[0-9]{4}(-)((0[1-9])|(1[0-2]))(-)((0[1-9])|([1-2][0-9])|(3[0-1]))(\s)(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])/;
据我所知,您在使用未初始化的值进行模式匹配时会遇到该错误。但是 为什么 当我 将正则表达式分配给变量时我得到它? 捕获组是否有什么导致 Perl 不合适?还是我漏掉了什么?
据我所知,这不会造成任何伤害,但它经常出现在我的日志中,我想将其清除。
/.../
是匹配运算符。它检查模式是否与绑定变量匹配。如果没有显式绑定变量,它将与 $_
匹配,就好像您使用了 $_ =~ /.../
.
您正在寻找
my $gdt_regex = qr/.../;
好吧,我认为一些验证会对您的代码有益
注意:可能 $date_start
和 date_end
更适合与 $date_field
use strict;
use warnings;
........
if (defined($passed_args->{'between'})) {
my($date_field,$date_start,$date_end) = @{$passed_args->{between}}[0..2];
{
$self->error('ERROR: between parameter not populated correctly.');
return;
} if ( !defined($passed_args->{$date_field})
&& !is_valid($date_start)
&& !is_valid($date_end)
)
$query_url .= $date_field . "BETWEEN" . $start_date . "@" . $end_date . "^";
delete $passed_args->{'between'};
}
#
# timestamp validation
#
sub is_valid {
my $timestamp = shift;
return 0 if $timestamp !~ /\d{4}-\d{2}-\d{} \d{2}:\d{2}:\d{2}/;
my($date,$time) = split ' ', $timestamp;
my($year,$month,$day) = split '-', $date;
my($hour,$min,$sec) = split ':', $time;
#return 0 if $year < 1900 or $year > 2019;
return 0 if $month < 1 or $month > 12;
return 0 if $day < 1 or $day > 31;
return 0 if $hour < 1 or $hour > 23;
return 0 if $min < 1 or $min > 59;
return 0 if $sec < 1 or $sec > 59;
return 1; # timestamp is valid
}