I am new to perl plack/psgi. I want to access a subroutine within the perl plack/psgi loop, but it looks like if the subroutine is not being executed. Every parent variable like $number should being passed automatically like when writing a regular perl script. Do I miss something here, is that possible?
..
my $app = sub {
my $number = 10;
&count_number;
sub count_number {
$number +=10;
}
return ['200',[ 'Content-Type' => 'application/json' ],
[ "{\"number\":$number} ]];
}
..
10 is being returned instead of 20 :(
If I fix the quotes on the string in the
returnstatement (you are missing a closing double-quote) then I get the warningThe reason is that lexical values
$appand$numberare defined at run time, whereas the subroutinecount_numberis defined much earlier during compilationThe solution is to defer definition of
count_numberto run time by making it an anonymous subroutine instead. The call$count_number->()also needs to be moved to after the definitionoutput
There is a related warning
with a similar solution. You can read about both in
perldoc perldiag. The messages are listed and described in alphabetical order