<div>Simplicidade é algo difícil de definir, mas todo mundo consegue identificar.</div><br>Vamos começar com um exemplo que eu considero simples:<br><br><br><font class="Apple-style-span" face="'courier new', monospace"> use Mojolicious::Lite;<br>
<br> get '/' => sub {<br> my $self = shift;<br> my $name = $self->param('name') || 'world';<br> $self->render( text => "Hello, $name" );<br>
};<br><br> app->start();</font><br><br><div><br><br>Esta é uma aplicação mínima, porém completa. Você pode executá-la e testá-la em <a href="http://localhost:3000">http://localhost:3000</a>.<br><br>Agora, vamos dizer que eu queira usa um template:<br>
<br><br><font class="Apple-style-span" face="'courier new', monospace"> use Mojolicious::Lite;<br><br> get '/' => sub {<br> my $self = shift;<br> my $name = $self->param('name') || 'world';<br>
<b>$self->stash( name => $name )</b>;<br> } <b>=> 'index';</b><br><br> app->start();<br><br><b> __DATA__<br> @@ index.html.ep<br> <h1>Hello, <%= $name %>!</h1></b></font><br>
<br><br></div><div><br>(Sob o ponto de vista didático é conveniene mostrar o template junto com o código, para que as pessoas possam copiar e colar; na prática eu criaria um arquivo dentro de um diretório chamado "templates", que seria reconhecido automaticamente.)</div>
<meta charset="utf-8"><div><br>E se eu quiser usar um arquivo de configuração? Basta criar um arquivo chamado "app.conf". (Onde "app" é igual ao nome da aplicação). Exemplo:<br><br><br><font class="Apple-style-span" face="'courier new', monospace"> {<br>
default_name => 'world'<br> }<br></font><br><br></div><div><br>Para usar o arquivo de configuração:<br><br><br><br><font class="Apple-style-span" face="'courier new', monospace"> use Mojolicious::Lite;<br>
<br><b> plugin 'config';<br></b><br> get '/' => sub {<br> my $self = shift;<br> my $name = $self->param('name') || <b>$self->config('default_name')</b>;<br>
$self->stash( name => $name );<br> } => 'index';<br><br> app->start();<br><br> __DATA__<br> @@ index.html.ep<br> <h1>Hello, <%= $name %>!</h1><br>
</font><br><br></div><div><br></div><div>Se você quiser acessar a variável de configuração a partir do template:<div><br></div><div><br></div><div><meta charset="utf-8"><span class="Apple-style-span" style="font-family: 'courier new', monospace; "> use Mojolicious::Lite;<br>
<br> plugin 'config';<br><br> get '/' => sub {<br> my $self = shift;<br> my $name = $self->param('name');<br> $self->stash( name => $name );<br>
} => 'index';<br><br> app->start();<br><br> __DATA__<br> @@ index.html.ep<br> <h1>Hello, <%= $name <b>|| config('default_name')</b> %>!</h1><br></span></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><br></div><div><br></div><div>Vamos, agora, guardar o nome em uma variável de sessão:</div><div><br></div><div><br></div><div>
<div><font class="Apple-style-span" face="'courier new', monospace"> use Mojolicious::Lite;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace"> plugin 'config';</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> </font></div><div><font class="Apple-style-span" face="'courier new', monospace"> get '/' => sub {</font></div><div>
<font class="Apple-style-span" face="'courier new', monospace"> my $self = shift;</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace"> my $name = $self->param('name')</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><b> || $self->session('name')</b></font></div><div><font class="Apple-style-span" face="'courier new', monospace"> || $self->config('default_name');</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace"><b> $self->session( name => $name );</b></font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace"> $self->stash( name => $name );</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> } => 'index';</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> app->start(); </font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> __DATA__</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> @@ index.html.ep</font></div><div>
<font class="Apple-style-span" face="'courier new', monospace"> <h1>Hello, <%= $name %>!</h1></font></div></div><div><br></div><div><br></div><div><br></div><div>Aí está: um exemplo completo, com template, arquivo de configuração externo, e sessão.</div>
<meta charset="utf-8"><div><br></div><div>Compare com a página de documentação do Catalyst::Plugin::Session:</div><div><br></div><div><meta charset="utf-8"><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: medium; "><pre class="sh_perl sh_sourceCode" style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(238, 238, 238); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(136, 136, 136); border-right-color: rgb(136, 136, 136); border-bottom-color: rgb(136, 136, 136); border-left-color: rgb(136, 136, 136); color: rgb(0, 0, 0); padding-top: 1em; padding-right: 1em; padding-bottom: 1em; padding-left: 1em; white-space: pre; font-weight: normal; font-style: normal; background-position: initial initial; background-repeat: initial initial; ">
<span class="sh_comment" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; "># To get sessions to "just work", all you need to do is use these plugins:</span>
<span class="sh_keyword" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">use</span> Catalyst <span class="sh_keyword" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">qw</span><span class="sh_symbol">/</span>
Session
Session<span class="sh_symbol">::</span>Store<span class="sh_symbol">::</span>FastMmap
Session<span class="sh_symbol">::</span>State<span class="sh_symbol">::</span>Cookie
<span class="sh_symbol">/;</span>
<span class="sh_comment" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; "># you can replace Store::FastMmap with Store::File - both have sensible</span>
<span class="sh_comment" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; "># default configurations (see their docs for details)</span>
<span class="sh_comment" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; "># more complicated backends are available for other scenarios (DBI storage,</span>
<span class="sh_comment" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; "># etc)</span>
<span class="sh_comment" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; "># after you've loaded the plugins you can save session data</span>
<span class="sh_comment" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; "># For example, if you are writing a shopping cart, it could be implemented</span>
<span class="sh_comment" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; "># like this:</span>
<span class="sh_keyword" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">sub</span> add_item <span class="sh_symbol">:</span> Local <span class="sh_cbracket">{</span>
<span class="sh_keyword" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">my</span> <span class="sh_symbol">(</span> <span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">$self</span><span class="sh_symbol">,</span> <span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">$c</span> <span class="sh_symbol">)</span> <span class="sh_symbol">=</span> <span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">@_</span><span class="sh_symbol">;</span>
<span class="sh_keyword" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">my</span> <span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">$item_id</span> <span class="sh_symbol">=</span> <span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">$c</span><span class="sh_symbol">-></span>req<span class="sh_symbol">-></span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">param</span><span class="sh_symbol">(</span><span class="sh_string" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">"item"</span><span class="sh_symbol">);</span>
<span class="sh_comment" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; "># $c->session is a hash ref, a bit like $c->stash</span>
<span class="sh_comment" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; "># the difference is that it' preserved across requests</span>
<span class="sh_keyword" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">push</span> @<span class="sh_cbracket">{</span> <span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">$c</span><span class="sh_symbol">-></span>session<span class="sh_symbol">-></span><span class="sh_cbracket">{</span>items<span class="sh_cbracket">}</span> <span class="sh_cbracket">}</span><span class="sh_symbol">,</span> <span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">$item_id</span><span class="sh_symbol">;</span>
<span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">$c</span><span class="sh_symbol">-></span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">forward</span><span class="sh_symbol">(</span><span class="sh_string" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">"MyView"</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span>
<span class="sh_keyword" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">sub</span> display_items <span class="sh_symbol">:</span> Local <span class="sh_cbracket">{</span>
<span class="sh_keyword" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">my</span> <span class="sh_symbol">(</span> <span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">$self</span><span class="sh_symbol">,</span> <span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">$c</span> <span class="sh_symbol">)</span> <span class="sh_symbol">=</span> <span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">@_</span><span class="sh_symbol">;</span>
<span class="sh_comment" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; "># values in $c->session are restored</span>
<span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">$c</span><span class="sh_symbol">-></span>stash<span class="sh_symbol">-></span><span class="sh_cbracket">{</span>items_to_display<span class="sh_cbracket">}</span> <span class="sh_symbol">=</span>
<span class="sh_symbol">[</span> <span class="sh_keyword" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">map</span> <span class="sh_cbracket">{</span> MyModel<span class="sh_symbol">-></span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">retrieve</span><span class="sh_symbol">(</span><span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">$_</span><span class="sh_symbol">)</span> <span class="sh_cbracket">}</span> @<span class="sh_cbracket">{</span> <span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">$c</span><span class="sh_symbol">-></span>session<span class="sh_symbol">-></span><span class="sh_cbracket">{</span>items<span class="sh_cbracket">}</span> <span class="sh_cbracket">}</span> <span class="sh_symbol">];</span>
<span class="sh_variable" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">$c</span><span class="sh_symbol">-></span><span class="sh_function" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">forward</span><span class="sh_symbol">(</span><span class="sh_string" style="color: rgb(0, 0, 0); font-weight: normal; font-style: normal; ">"MyView"</span><span class="sh_symbol">);</span>
<span class="sh_cbracket">}</span></pre></span></div><div><br></div><div>Este é o "problema de complexidade".</div><div><br></div><div>Se o Catalyst::Lite puder reunir a simplicidade do Mojolicious com a solidez do Catalyst, creio que este será o melhor dos dois mundos.</div>
<div><br></div><div>[]s</div><div><br></div><div>Nelson</div><div><br></div></div>