“无法调用方法“dir_path”在未定义的值上”在命令行运行 Mason 组件时

发布于 2024-08-10 20:58:03 字数 1390 浏览 3 评论 0原文

您好,

我正在尝试为 Mason 组件开发一些测试,这需要在命令行而不是 Web 服务器上运行它们。当我尝试此操作时,出现错误:

perl -MHTML::Mason::Request -MHTML::Mason::Interp -I./lib \
-e '$int = HTML::Mason::Interp->new( data_dir => "/home/friedo/cache", comp_root => "/home/friedo/comps" ); $m = HTML::Mason::Request->new( comp => "/dummy", interp => $int ); $m->comp("/dummy")'

结果:

Can't call method "dir_path" on an undefined value at lib/HTML/Mason/Request.pm line 1123.

尝试调用 ->comp 时抛出错误。我无法弄清楚配置有什么问题。该组件已经存在并且看起来编译得很好,并且它通过 Apache 运行。

这是使用 HTML::Mason 1.35。

编辑:让我们尝试一下这个赏金。另一种选择是我必须深入梅森的内心! :)

再次编辑:非常感谢David指出我错过的关键细节为了让它发挥作用。

这实际上是针对一个测试框架,该框架需要执行一个调用某些 Mason comps 的模块——在正常操作下,该模块提供了一个 Mason 请求对象以用于该目的,但我无法让它离线工作。关键是使用 Interpreter 对象,所以我最终执行了以下操作,这有点愚蠢,但使测试有效:

sub _mason_out { 
   ...
   my $buf;
   if ( $ENV{MASON_TEST} ) { 
       my $int = HTML::Mason::Interp->new( comp_root  => $self->{env}->comp_dir,
                                           out_method => \$buf );

       $int->exec( $comp, %args );
   } else { 
       my $m = $self->{mason_object};
       $m->comp( { store => \$buf }, $comp, %args );
   }

   return $buf;
}

Greetings,

I'm trying to develop some tests for Mason components which requires running them on the command line instead of the web server. When I try this, I get an error:

perl -MHTML::Mason::Request -MHTML::Mason::Interp -I./lib \
-e '$int = HTML::Mason::Interp->new( data_dir => "/home/friedo/cache", comp_root => "/home/friedo/comps" ); $m = HTML::Mason::Request->new( comp => "/dummy", interp => $int ); $m->comp("/dummy")'

Results in:

Can't call method "dir_path" on an undefined value at lib/HTML/Mason/Request.pm line 1123.

The error is thrown when the call to ->comp is attempted. I can't figure out what's wrong with the configuration. The component is there and appears to be compiled just fine, and it works via Apache.

This is using HTML::Mason 1.35.

Edit: Let's try a bounty for this one. The alternative is me having to dive deep into Mason's guts! :)

Edit again: Thanks very much to David for pointing out the crucial detail that I missed for getting this to work.

This was actually for a test framework that needed to exercise a module that calls some Mason comps -- under normal operation the module is provided with a Mason request object to use for that purpose, but I couldn't get that to work offline. The key was using an Interpreter object instead, so I ended up doing the following, which is a little silly but makes the tests work:

sub _mason_out { 
   ...
   my $buf;
   if ( $ENV{MASON_TEST} ) { 
       my $int = HTML::Mason::Interp->new( comp_root  => $self->{env}->comp_dir,
                                           out_method => \$buf );

       $int->exec( $comp, %args );
   } else { 
       my $m = $self->{mason_object};
       $m->comp( { store => \$buf }, $comp, %args );
   }

   return $buf;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱她像谁 2024-08-17 20:58:03

我认为这会失败,因为您的 Request 对象在调用时尚未构建组件堆栈。请使用 Interp->exec() 方法,如 从独立脚本使用 Mason

perl -MHTML::Mason::Interp -I./lib \
-e 'HTML::Mason::Interp->new( data_dir => "/home/friedo/cache", comp_root => "/home/friedo/comps" )->exec("/dummy")'

I think this fails because your Request object hasn't built a component stack at the point that it is called. Use the Interp->exec() method instead as described in Using Mason from a Standalone Script

perl -MHTML::Mason::Interp -I./lib \
-e 'HTML::Mason::Interp->new( data_dir => "/home/friedo/cache", comp_root => "/home/friedo/comps" )->exec("/dummy")'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文