Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl Plack::Builder-最后一行不是';t使用mount-错误消息_Perl_Plack - Fatal编程技术网

Perl Plack::Builder-最后一行不是';t使用mount-错误消息

Perl Plack::Builder-最后一行不是';t使用mount-错误消息,perl,plack,Perl,Plack,拥有下一个简单的Plack应用程序: use strict; use warnings; use Plack::Builder; my $app = sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello World' ] ]; }; builder { foreach my $act ( qw( /some/aa /another/bb / ) ) { mount

拥有下一个简单的Plack应用程序:

use strict;
use warnings;
use Plack::Builder;

my $app = sub { 
        return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello World' ] ]; 
};

builder {
    foreach my $act ( qw( /some/aa /another/bb  / ) ) {
        mount $act => $app;
    }
};
返回错误:

WARNING: You used mount() in a builder block, but the last line (app) isn't using mount().
WARNING: This causes all mount() mappings to be ignored.
 at /private/tmp/test.psgi line 13.
Error while loading /private/tmp/test.psgi: to_app() is called without mount(). No application to build. at /private/tmp/test.psgi line 13.
但是下一个构建块是可以的

builder {
    foreach my $act ( qw( /some/aa /another/bb  / ) ) {
        mount $act => $app;
    }
    mount "/" => $app;
};
我比他说的还要明白

注意:在生成器代码中使用mount后,必须使用mount 对于所有路径,包括根路径(/)

但是在
for
循环中,我将
/
装载为最后一个:
qw(/some/aa/other/bb/)
,所以在场景后面有一些东西

有人能解释一下吗?

看一下帮助,了解发生了什么:

sub builder(&) {
    my $block = shift;
    ...
    my $app = $block->();

    if ($mount_is_called) {
        if ($app ne $urlmap) {
            Carp::carp("WARNING: You used mount() in a builder block,
因此,
builder
只是一个子例程,它的参数是一个代码块。对该代码块进行求值,结果最终显示在
$app
中。但是,对于您的代码,计算的结果是空字符串,它是终止
foreach
循环的结果:

$ perl -MData::Dumper -e 'sub test{ for("a", "b"){ $_ } }; print Dumper(test())'
$VAR1 = '';
由于
mount foo=>$bar
是一种“仅仅”的语法糖,在像您这样的情况下甚至很难阅读,因此我建议您向裸机迈出一小步,跳过语法糖,直接使用:

use strict;
use warnings;
use Plack::App::URLMap;

my $app = sub { 
    return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello World' ] ]; 
};

my $map = Plack::App::URLMap->new;

foreach my $act ( qw( /some/aa /another/bb  / ) ) {
    $map->mount( $act => $app );
}

$map->to_app;