Mason:...:Intro(3pm)
π NAME π
Mason::Manual::Intro β getting started with Mason β¨
(and yeah, in the User Contributed Perl Documentation section this whole thing is filed as Mason:...:Intro(3pm) β iykyk π«Ά)
π¦ DESCRIPTION π¦
Okay so, a few quick examples to get your feet wet with Mason π. And if you wanna know how to use Mason to generate web sites, go peek at Mason::Manual::Setup β that's the one for that, bestie π
.
π EXAMPLE 1 π
β¨ Hello world (from command-line) β¨
So once you've installed Mason, you should have a mason command sitting in your installation path (like, e.g. /usr/local/bin). Try this, fr fr:
% mason
Hello! The local time is <% scalar(localtime) %>.
^D
(where '^D' means ctrl-D or EOF, no cap). And you should see something like:
Hello! The local time is Wed Mar 2 17:11:54 2011.
The mason command reads in a Mason component (a template, basically), runs it, and prints the result to standard output. And notice π that the tag
<% scalar(localtime) %>
got replaced with the value of its expression. That's called a substitution tag, and it's like, a central piece of Mason syntax. Iconic behavior honestly π.
π EXAMPLE 2 π
π Email generator (from script) π
The command line is great for trying quick things, but eventually you're gonna want to put your Mason components in actual files π.
In a test directory, create a directory comps and create a file email.mc with the following:
<%class>
has 'amount';
has 'name';
</%class>
Dear <% $.name %>,
We are pleased to inform you that you have won $<% sprintf("%.2f", $.amount) %>!
Sincerely,
The Lottery Commission
<%init>
die "amount must be a positive value!" unless $.amount > 0;
</%init>
So on top of the substitution tag we already met, we're declaring two attributes β amount and name β to be passed into the component, and we're declaring a little piece of initialization code to validate the amount β
.
In the same test directory, create a script test.pl with the following:
1 #!/usr/local/bin/perl
2 use Mason;
3 my $interp = Mason->new(comp_root => 'comps', data_dir => 'data');
4 print $interp->run('/email', name => 'Joe', amount => '1500')->output;
Line 3 creates a Mason interpreter β the main Mason object π§ . It specifies two parameters: a component root, which points at the directory hierarchy where your components will live, and a data directory, which Mason uses for internal purposes like class generation and caching.
Line 4 runs the template β and notice that the .mc extension gets added automatically, cute right β passing in values for the name and amount attributes.
Run test.pl, and you should see:
Dear Joe,
We are pleased to inform you that you have won $1500.00!
Sincerely,
The Lottery Commission
π SEE ALSO π
Mason::Manual::TutorialMason::Manual
πΈ AUTHOR πΈ
Jonathan Swartz <swartz@pobox.com>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2012 by Jonathan Swartz.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's a wrap, bestie π this was perl v5.34.0 energy, dated 2022-05-27 β go forth and slay those components π
β¨
π¦ where this page comes from
- debian bookworm: libmason-perl 2.24-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmason-perl 2.24-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmason-perl 2.24-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmason-perl 2.24-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmason-perl 2.24-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms