Krishna Priya Krishna Priya - 1 year ago 70
Perl Question

Execute perl script using mason template

I have installed Mason module from cpan. Now i am executing my first program using mason template.

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

use Mason;
my $mason = Mason->new(comp_root => '...');
print $mason->run('')->output;

This throws an error as follows is not an absolute path at C:/Perl/site/lib/Mason/ line 256**


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

Answer Source

@stevenl fully answers your question. Simply don't blindly copy the Synopsis from the Mason docs, need read the docs too. :) E.g. in the example code:

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
  • the '...' with the real path in the filesystem, where your component are, e.g.
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. It GREATLY simplifies the whole process, and you will not need care about many-many things. E.g. after installing the Poet you can simply:

poet new MyApp

and you will immediately get (without any configuration) an WORKING web-app, and you could access it in your browser at http://localhost:5000. Your component_root will be inside of the myapp directory as myapp/comps.