I'm new to Mason2/POET and I have been using this guide http://search.cpan.org/~jswartz/Poet/lib/Poet/Manual/Tutorial.pod to create my first website.
Now I would like to create a new global variable (example: $User) but then I have no idea or what direction I should take in order to do so since the document doesnt explain about it. Most documents I found were about Apache or mod_perl...
Example of what I'm looking for:
<%augment wrap>
<html>
html code goes here
</html>
</%augment>
<%init>
my $User;
Mason::Interp::allow_globals => [qw($User)];
</%init>
Just read the Poet::Import.
Simple example:
add a class
My::Import
, e.g.and add into it
e.g. the
Poet::Import
already importing variables$conf
and$env
(and also the utility tag:web
. So, you just extends thePoet::Import
, by adding another "attribute" (your "variable") into it.In the above example
mytemp
$mytemp
.Now, you can use it for example in your components. Edit your
comps/index.mc
.Into the top add
and add following too:
The
$mytemp
using theuse Poet qw($mytemp);
is imported from yourMy/Import.pm
. (it is read-only, by it's definition -(is => 'ro',...
).Everything in the
Poet/Mason
isMoose
:), so (of course) you can importrw
variable with anyisa
... etc.Just remember, the above is true global and persistent variable. E.g. its content is preserved between requests. In most cases you don't want use such variables, only in few special cases, for example, you want initialise some database handle
$dbh
(what should be available util the application runs) and such.Second, here is also the
$m->notes
method, but don't over-use it. From the docs:Mostly, it is enough to use simple component attributes, e.g. see for example in the generated default app the usage of the
$.title
(e.g. $self->title).Or you just can pass variables to components as arguments,
and so on...
Again, every
component
is::)