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 &quot;the stash&quot;.
Since it is a non-blocking framework, your code can&#39;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&#39;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-&gt;under(&#39;/protected&#39; =&gt; sub {
  # check authentication
});

# /protected/safe
$protected-&gt;get(&#39;/safe&#39; =&gt; &#39;safe&#39;);

$r-&gt;get(&#39;/another_unsafe&#39; =&gt; ...);
</code></pre>

<p>Since Lite app keywords don&#39;t have a way to attach to another route, they basically always add them to the &quot;current global route&quot;.
That&#39;s where the confusion comes in.</p>

<p>Speaking of which, I&#39;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-&gt;routes</code>.
Then, I use that instead of the routing keywords in all but the simplest of cases.</p>

<h2>Conclusion</h2>

<p>That&#39;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 )