Acme-CPANModulesBundle-Import-MojoliciousAdvent-2017
view release on metacpan or search on metacpan
devdata/https_mojolicious.io_blog_2017_12_02_day-2-the-stash view on Meta::CPAN
<div class="post-thumb">
<!-- theme suggests 1300x500 -->
<img alt="leather bag" src="/blog/2017/12/02/day-2-the-stash/bag-1854148_1920.jpg">
</div>
<div class="post-content">
<section id="section-1">
<p>In Mojolicious, when processing a request and preparing a response one of the most important concepts is "the stash".
Since it is a non-blocking framework, your code can't use global variables to store any state during processing.
If you did and some other code were to run, it could very easily get cross-talk between requests.</p>
<p>The stash is the place you can store information while you process it.
It is just a simple hash reference that is attached to the controller object that is processing the request.
It lives and dies with that one transaction.</p>
<p>While you can and should use it as a scratchpad, it really is much more.
The stash controls almost every aspect of the response that you generate.
Let's look a little closer to see how it works</p>
devdata/https_mojolicious.io_blog_2017_12_04_day-4-dont-fear-the-full-app view on Meta::CPAN
my $protected = $r->under('/protected' => sub {
# check authentication
});
# /protected/safe
$protected->get('/safe' => 'safe');
$r->get('/another_unsafe' => ...);
</code></pre>
<p>Since Lite app keywords don't have a way to attach to another route, they basically always add them to the "current global route".
That's where the confusion comes in.</p>
<p>Speaking of which, I'm going to let you in even deeper on my secret.
I like the chained type of routing so much more than using <code>group</code> that I actually use it in my Lite apps.
Sure I still use <code>app</code> and <code>plugin</code>, but one of the first things I do is <code>my $r = app->routes</code>.
Then, I use that instead of the routing keywords in all but the simplest of cases.</p>
<h2>Conclusion</h2>
<p>That's it, with the exception of using <code>group</code> for nested routing, it is just direct translation.
( run in 0.723 second using v1.01-cache-2.11-cpan-49f99fa48dc )