marat75 marat75 - 6 months ago 19
Perl Question

Syntax errors while trying to change a foreach loop

I am very new to Perl and I am trying to learn enough of it to refactor a few lines of legacy code.

Here is a snippet of the code that I have:

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


What I tried to do is this:

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

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


Any explanation of what I am doing wrong and how to achieve what I need (retrieving the last item from the loop and assigning it to $app instead of iterating through the whole loop) would be appreciated.

I have weird errors in the logs and the page won't load. Here is what the error log says:

[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


Nevermind, after apache restart #9 it finally worked.

Answer

$data2 is a reference to a hash. The pop() function takes an array as an argument. Because $data2 references a hash, there is no concept of a "last" element to remove. So you're going to need to rethink what it is you're trying to do.

In your case, the loop was iterating over the array referenced by $data2->{record}. You can access the last element of that with pop, however:

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