perlcpanmason

Execute perl script using mason template


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

first_mason.mc

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

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**

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


Solution

  • @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:

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

    you need replace

    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
    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. Your component_root will be inside of the myapp directory as myapp/comps.