我不知道如何使用Perl从URL中获取查询字符串。我尝试了几种方法,例如:
my $qs = $ENV{'QUERY_STRING'};
my @d = split(/&/, $qs);
if ( $d[1] eq 'reports' ) {
... do something
}
并且
use CGI;
my $q = CGI->new;
my $page = $q->param('page');
但是我无法获得键的值。现在我手动将查询字符串放入其中,即
project.local/routes.cgi?page=reports
但是我从命令行中得到以下结果:Use of uninitialized value $qs in split at ./routes.cgi line 23.
Use of uninitialized value $d[1] in string eq at ./routes.cgi line 25.
我不确定为什么会有这个警告,因为如果它不存在,它应该只是未定义的吧?
我应该如何存储和检查查询字符串变量?我主要使用routes.cgi脚本作为路由控制器,所有链接都指向i.e. <a href="project.local/routes.cgi?page=xx">
,然后我处理一个模板工具包文件并重定向到该页面(这个概念好吗?)
诊断结果:
Use of uninitialized value $qs in split at ./routes.cgi line 23 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
编辑:
use URI qw( );
use URI::QueryParam qw( );
my $u = URI->new($base_url);
my ($p) = $u->query_param('page');
if ( $p eq 'reports' ) {
...do something
}