尝试更改foreach循环时出现语法错误

时间:2016-05-10 20:56:12

标签: perl

我是Perl的新手,我正在努力学习它,以重构几行遗留代码。

以下是我所拥有的代码片段:

my ( $data2 ) = @_;
foreach $app (@{$data2->{record}})
{
...
    if ( $app->{submission_date__required_} )
    {
            $datetime = convert_date( $app->{submission_date__required_}, "both" );
            print $datetime;
    }
}

我试图做的是:

my $data = pop $data2;
my $app = $data->{record};

if ( $app->{submission_date__required_} )
{
    $datetime = convert_date( $app->{submission_date__required_}, "both" );
    print $datetime;
}

对我做错了什么以及如何实现我所需要的任何解释(从循环中检索最后一项并将其分配给$ app而不是遍历整个循环)将不胜感激。

我在日志中遇到奇怪的错误,页面无法加载。以下是错误日志所说的内容:

[Tue May 10 14:55:20 2016] [error] [client xxx.xxx.xxx.xxx] Use of uninitialized value in lc at submitapp.pl line 48.
[Tue May 10 14:55:20 2016] [error] [client xxx.xxx.xxx.xxx] Use of uninitialized value $notix in print at submitapp.pl line 177.
[Tue May 10 14:55:20 2016] [error] [client xxx.xxx.xxx.xxx] Use of uninitialized value $store in string eq at submitapp.pl line 249.
[Tue May 10 14:55:20 2016] [error] [client xxx.xxx.xxx.xxx] Use of uninitialized value $store in string eq at submitapp.pl line 258.
[Tue May 10 14:55:20 2016] [error] [client xxx.xxx.xxx.xxx] Use of uninitialized value $store in string eq at submitapp.pl line 267.
[Tue May 10 14:55:20 2016] [error] [client xxx.xxx.xxx.xxx] Use of uninitialized value $operating_mode in string eq at submitapp.pl line 287.
[Tue May 10 14:55:20 2016] [error] [client xxx.xxx.xxx.xxx] Use of uninitialized value $operating_mode in string eq at submitapp.pl line 300.
[Tue May 10 14:55:20 2016] [error] [client xxx.xxx.xxx.xxx] Use of uninitialized value $operating_mode in string eq at submitapp.pl line 328.
[Tue May 10 15:09:10 2016] [error] [client xxx.xxx.xxx.xxx] syntax error at getqbdata.pl line 415, near "), referer: http://xxx.xxx.xxx.xxx/qb/submitapp.pl?action=Submit
[Tue May 10 15:09:10 2016] [error] [client xxx.xxx.xxx.xxx] \t\tprint", referer: http://xxx.xxx.xxx.xxx/qb/submitapp.pl?action=Submit
[Tue May 10 15:09:10 2016] [error] [client xxx.xxx.xxx.xxx] Execution of getqbdata.pl aborted due to compilation errors., referer: http://xxx.xxx.xxx.xxx/qb/submitapp.pl?action=Submit
[Tue May 10 15:09:10 2016] [error] [client xxx.xxx.xxx.xxx] Premature end of script headers: getqbdata.pl

没关系,在apache重新启动#9后它终于奏效了。

1 个答案:

答案 0 :(得分:1)

$data2是对哈希的引用。 pop()函数将数组作为参数。因为$data2引用了一个哈希,所以没有" last"要删除的元素。因此,您需要重新考虑您正在尝试做的事情。

在您的情况下,循环迭代$data2->{record}引用的数组。您可以使用pop访问 的最后一个元素,但是:

my $app = pop @{$data2->{record}};