Mason:...:Intro(3pm)

πŸ“¦
version 2.24 Β· 2 versions
  • 2.24 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 2.24 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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::Tutorial
  • Mason::Manual

🌸 AUTHOR 🌸

Jonathan Swartz <swartz@pobox.com>


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