简体   繁体   English

使用梅森模板执行Perl脚本

[英]Execute perl script using mason template

I have installed Mason module from cpan. 我已经从cpan安装了Mason模块。 Now i am executing my first program using mason template. 现在,我正在使用梅森模板执行我的第一个程序。

first_mason.mc first_mason.mc

% my $name = "Mason";
    Hello world! Welcome to <% $name %>. 

first_mason.pl first_mason.pl

#!/usr/local/bin/perl
use Mason;
my $mason = Mason->new(comp_root => '...');
print $mason->run('first_mason.mc')->output;

This throws an error as follows 这将引发如下错误

first_mason.mc is not an absolute path at C:/Perl/site/lib/Mason/Request.pm line 256** first_mason.mc不是C:/Perl/site/lib/Mason/Request.pm第256行上的绝对路径**

Note 注意

I am placing both files in the path where mason is installed(to find an installation path ,i used perldoc -l Mason ) and executed a program using perl first_mason.pl 我将两个文件都放置在安装了mason的路径中(以找到安装路径,我使用perldoc -l Mason )并使用perl first_mason.pl执行了程序

There is no need to put your files in the directory where Mason is installed: 无需将文件放在Mason的安装目录中:

  • Perl should know where to find Mason when you import it with use (assuming your perl installation is correct). use导入时,Perl应该知道在哪里可以找到Mason(假设您的Perl安装正确)。
  • Mason will know where to find the .mc file via the comp_root argument. Mason将通过comp_root参数知道在哪里找到.mc文件。
  • The component name needs to be specified as a path relative to comp_root , always beginning with / . 需要将组件名称指定为相对于comp_root的路径,始终以/开头。
  • You need to leave out the .mc from the component name. 您需要从组件名称中删除.mc

So, if you place the 2 files in your home directory, then the script should look like this: 因此,如果将2个文件放在主目录中,则脚本应如下所示:

#!/usr/local/bin/perl
use Mason;
my $mason = Mason->new(comp_root => $HOME_DIR); # where $HOME_DIR is `C:\User\your_name`
print $mason->run('/first_mason')->output;

From the documentation : 文档中

The component root and component paths 组件根目录和组件路径

When you use Mason, you specify a component root that all component files live under. 使用Mason时,可以指定所有组件文件都位于其下的组件根目录。 Thereafter, any component will be referred to by its virtual path relative to the root, rather than its full filename. 此后,将通过相对于根的虚拟路径而不是其完整文件名来引用任何组件。

For example, if the component root is '/opt/web/comps', then the component path '/foo/bar.mc' refers to the file '/opt/web/comps/foo/bar.mc'. 例如,如果组件根目录为“ / opt / web / comps”,则组件路径“ /foo/bar.mc”将引用文件“ /opt/web/comps/foo/bar.mc”。

@stevenl fully answers your question. @stevenl会完全回答您的问题。 Simply don't blindly copy the Synopsis from the Mason docs, need read the docs too. 只是不要盲目的从Mason文档中复制大纲,也需要阅读文档。 :) Eg in the example code: :)例如示例代码:

#!/usr/local/bin/perl
use Mason;
my $mason = Mason->new(comp_root => '...');
print $mason->run('/foo')->output;

you need replace 你需要更换

  • and the shebang line #!/usr/local/bin/perl with the real path to your perl interpreter shebang行 #!/usr/local/bin/perl以及您的perl解释器的真实路径
  • the '...' with the real path in the filesystem, where your component are, eg 文件系统中实际路径的'...' ,您的组件所在的位置,例如
comp_root => '/some/real/path/here/where/my/component/root/is'

However, I wrote this answer mainly with a reason: if you want use the Mason for the web-app development, check the Poet module too. 但是,我写这个答案的原因主要是:如果要使用Mason进行Web应用程序开发,请同时检查Poet模块。 It GREATLY simplifies the whole process, and you will not need care about many-many things. 它极大地简化了整个过程,您无需担心很多事情。 Eg after installing the Poet you can simply: 例如,在安装Poet之后,您可以简单地:

poet new MyApp
myapp/bin/run.pl

and you will immediately get ( without any configuration) an WORKING web-app, and you could access it in your browser at http://localhost:5000 . 然后您将立即获得( 无需任何配置)WORKING Web应用程序,并且可以在浏览器中通过http:// localhost:5000对其进行访问 Your component_root will be inside of the myapp directory as myapp/comps . 您的component_root将以myapp/comps的形式位于myapp目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM