Global Variable mason2 in POET
Asked Answered
T

1

6

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>
Thebaid answered 2/2, 2015 at 15:33 Comment(4)
You usually will not need any global variable, but if you want some check the Poet::Import and the provide_var_varname method.Microanalysis
@jm666 Poet::Import looks promising, can you elaborate your comment a little more? why you usually won't need any global variable, and what to do if you want to use anyway? I just started a bounty on this question... tnxConnel
@Connel missed your bounty, :) but added an more detailed answer anyway - hope helps.Microanalysis
@jm666 thank you for your answer, I won't be able to try it for a while but I will let you know as soon as possible, it looks promisingConnel
M
3

Just read the Poet::Import.

Simple example:

# generate app My
poet new my
cd my

add a class My::Import, e.g.

vi lib/My/Import.pm

and add into it

package My::Import;
use Poet::Moose;
extends 'Poet::Import';

use Types::Path::Tiny qw(Path);

# create some variable
has 'mytemp' => (is => 'ro', isa => Path, coerce => 1, default => '/tmp');

method provide_var_mytemp ($caller) { #your WANTED variable name - add after the "provide_var_"
    return $self->mytemp;
}
1; #happy perl

e.g. the Poet::Import already importing variables $conf and $env (and also the utility tag :web. So, you just extends the Poet::Import, by adding another "attribute" (your "variable") into it.

In the above example

  • added the attribute mytemp
  • and want call the global variable as $mytemp.

Now, you can use it for example in your components. Edit your comps/index.mc.

Into the top add

<%class>
use Poet qw($mytemp);  #your global variable (is a Path::Tiny object to /tmp)
</%class>

and add following too:

<h1>My files in the /tmp</h1>
<pre>
% for my $file ($mytemp->children) {
        <% $file %>
% }
</pre>

The $mytemp using the use Poet qw($mytemp); is imported from your My/Import.pm. (it is read-only, by it's definition - (is => 'ro',...).

Everything in the Poet/Mason is Moose :), so (of course) you can import rw variable with any isa... 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:

The notes() method provides a place to store application data between components - essentially, a hash which persists for the duration of the request.

Consider storing this kind of data in a read-write attribute of the page component.

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,

<& somecomp.mc, arg1 => 'some', arg2 => 'other' &>

and so on...

Again, every component is:

  • just a camel
  • with antlers
  • using some masonry tools
  • in poetic environment
  • at the top of the PSGI hill

:)

Microanalysis answered 1/7, 2015 at 10:56 Comment(3)
thank you for your answer. This is what I was looking for. Since you missed the bounty, I just started anoter bounty and I will reward as soon as i can.Connel
This is the kind of interaction that makes SO great :)Fulgurant
@Connel woot! Thanx for the bounty. ;)Microanalysis

© 2022 - 2024 — McMap. All rights reserved.