Mason2 global variable in POET

I'm new to Mason2 / POET, and I used this http://search.cpan.org/~jswartz/Poet/lib/Poet/Manual/Tutorial.pod guide to create my first website.

Now I would like to create a new global variable (example: $ User), but then I have no idea what direction I should take in order to do this, since the document does not explain it. Most of the documents I found related to Apache or mod_perl ...

An 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> 
+6
source share
1 answer

Just read Poet :: Import .

A simple example:

 # generate app My poet new my cd my 

add class My::Import , for example

 vi lib/My/Import.pm 

and add to 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 

eg. Poet::Import already imports the variables $conf and $env (as well as the utility tag :web ). Thus, you simply extend Poet::Import by adding another “attribute” (your “variable”) to it.

In the example above

  • added mytemp attribute
  • and want to call a global variable like $mytemp .

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

Add to top

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

and add the following:

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

$mytemp $mytemp imported from My/Import.pm . (this is read-only, by definition - (is => 'ro',... ).

Everything is in Poet/Mason Moose :), so (of course) you can import the rw variable with any isa ... etc.

Just remember that this is a true global and constant variable. For instance. its contents are stored between requests. In most cases, you do not want to use such variables, only in a few special cases, for example, you want to initialize some $dbh database descriptor (which should be accessible by running applications), etc.

Secondly, the $m->notes method is also used here, but do not overwork it. From docs :

The notes () method provides a place to store application data between components - essentially a hash that remains throughout the request.

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

Basically, it’s enough to use simple component attributes, for example. see, for example, in a generated application, the default use of $.title (for example, $ self-> title).

Or you can just pass variables to the components as arguments,

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

etc.

Again, each component :

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

:)

+3
source

Source: https://habr.com/ru/post/981879/


All Articles