Acme-CPANModulesBundle-Import-MojoliciousAdvent-2017

 view release on metacpan or  search on metacpan

devdata/https_mojolicious.io_blog_2017_12_03_day-3-using-named-routes  view on Meta::CPAN

</p>
</code></pre>

<p>And we can see that <a href="http://mojolicious.org/perldoc/Mojolicious/Plugin/TagHelpers#link_to"><code>link_to</code></a> generates link tags using route names.
The first argument is the text that will be displayed by the link.
The following arguments build the url by name and placeholder value(s).
The return is actually a <a href="http://mojolicious.org/perldoc/Mojo/URL">Mojo::URL</a> so if you need to add other things like query parameters, that can be done afterwards.</p>

<h2>Changing the Routes</h2>

<p>Everything was fine until one day Santa decided that he wanted show off some of the year&#39;s new toys too.
The problem was that he hadn&#39;t planned his urls for that.
He could just keep all the existing ones and add a special cased <code>/toys</code> route, but it would look silly that way.
What he really needed to do was move all of the staff pages to keep the urls consistent.</p>

<pre><code>use Mojolicious::Lite;

get &#39;/toy/:toy_name&#39; =&gt; {template =&gt; &#39;toy&#39;} =&gt; &#39;toy&#39;;
get &#39;/meet/:name&#39; =&gt; {template =&gt; &#39;staff&#39;} =&gt; &#39;staff&#39;;
get &#39;/&#39; =&gt; {template =&gt; &#39;home&#39;} =&gt; &#39;home&#39;;

devdata/https_mojolicious.io_blog_2017_12_11_day-11-useragent-content-generators  view on Meta::CPAN

  &quot;name&quot;: &quot;myObject&quot;
}

--foo_bar_baz
Content-Type: image/jpeg

[JPEG_DATA]
--foo_bar_baz--
</code></pre>

<p>While this was possible using the lower level tools, we decided that adding a generator for it would make using that API much easier for them.
Thus the <code>multipart</code> generator was added to the mix.
Using it, one can make a compliant request by writing something like</p>

<pre><code class="hljs"><span class="hljs-keyword">use</span> <span class="hljs-function">Mojo::Base</span> -strict;
<span class="hljs-keyword">use</span> <span class="hljs-function">Mojo::UserAgent</span>;
<span class="hljs-keyword">use</span> <span class="hljs-function">Mojo::JSON</span> &#39;<span class="hljs-string">encode_json</span>&#39;;
<span class="hljs-keyword">use</span> <span class="hljs-function">Mojo::File</span>;

<span class="hljs-keyword">my</span> <span class="hljs-type">$url</span>   = &#39;<span class="hljs-string">https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart</span>&#39;;
<span class="hljs-keyword">my</span> <span class="hljs-type">$token</span> = &#39;<span class="hljs-string">XXXXXXXXXX</span>&#39;;

devdata/https_mojolicious.io_blog_2017_12_11_day-11-useragent-content-generators  view on Meta::CPAN


<p>To motivate this discussion, I&#39;ll introduce another module.
At work, I had to use <a href="http://xmlrpc.scripting.com/spec.html">XML-RPC</a> to interact with a remote service.
XML-RPC defines an XML schema for asking the service to call a method, just as you would locally, by method name and with some arguments.
It then returns a result or fault (exception).
These responses also contain arguments, that is to say, the response data.</p>

<p>Personally I find it is much easier to learn something new by seeing how it works.
I pulled <a href="https://metacpan.org/pod/XMLRPC::Fast">XMLRPC::Fast</a> from CPAN and started inspecting the code.
It started to make sense to me, but I noticed that it used <a href="https://metacpan.org/pod/XML::Parser">XML::Parser</a> for its XML.
Since Mojolicious has tools for that, I decided to continue learning by porting the code to <a href="http://mojolicious.org/perldoc/Mojo/Template">Mojo::Template</a> and <a href="http://mojolicious.org/perldoc/Mojo/DOM">Mojo::DOM</a>.</p>

<p>By the time I finished I had completely rewritten the module and decided that perhaps others would benefit from it in environments already using the Mojo stack.
So with much thanks to XMLRPC::Fast and its author Sébastien Aperghis-Tramoni I released my own as <a href="https://metacpan.org/pod/Mojo::XMLRPC">Mojo::XMLRPC</a>.
My module (as did the original) only has functions that build the XML payloads.
Therefore, to make a simple request, pass the result of encoding as XML-RPC as the body of a request, like so</p>

<pre><code class="hljs"><span class="hljs-keyword">use</span> <span class="hljs-function">Mojo::Base</span> -strict;
<span class="hljs-keyword">use</span> <span class="hljs-function">Mojo::UserAgent</span>;
<span class="hljs-keyword">use</span> <span class="hljs-function">Mojo::XMLRPC</span> qw[encode_xmlrpc decode_xmlrpc];

<span class="hljs-keyword">my</span> <span class="hljs-type">$ua</span> = <span class="hljs-function">Mojo::UserAgent</span>-&gt;new;
<span class="hljs-keyword">my</span> <span class="hljs-type">$tx</span> = <span class="hljs-type">$ua</span>-&gt;<span class="hljs-type">post</span>(

devdata/https_mojolicious.io_blog_2017_12_12_day-12-more-than-a-base-class  view on Meta::CPAN

<pre><code>use Mojo::Base -strict;
</code></pre>

<p>and you get everything listed above.
Otherwise, if you use the class or roles functionality then these imports come along for free.</p>

<h3>Experimental Signatures</h3>

<p>In the past few years, Perl has added <a href="https://metacpan.org/pod/perlsub#Signatures">signatures</a> to subroutines as an <a href="https://metacpan.org/pod/perlexperiment">experimental</a> feature.
With Mojolicious&#39; emphasis on non-blocking functionality, and the frequent use of callbacks that that entails, the Mojo community has been especially anxious to use them.
However since these are still experimental, and are still subject to change, when Mojo::Base recently added this functionality, it was decided that it should be an additional opt-in flag.
Using it, suddenly</p>

<pre><code>use Mojo::Base -strict;
$ua-&gt;get(&#39;/url&#39; =&gt; sub {
  my ($ua, $tx) = @_;
  ...
});
</code></pre>

<p>becomes</p>

devdata/https_mojolicious.io_blog_2017_12_16_day-16-the-secret-life-of-sessions  view on Meta::CPAN


<p>This is a good time to mention <a href="http://mojolicious.org/perldoc/Mojolicious/Guides/Cookbook#Hypnotoad"><code>hypnotoad</code></a>.
It is Mojolicious&#39; recommended production application server.
It has many of the same characteristics as the <a href="http://mojolicious.org/perldoc/Mojolicious/Guides/Cookbook#Pre-forking"><code>prefork</code></a> server but with some tuned settings and one killer feature, <a href="http://mojolicious.org/perld...

<p>Note that on native Windows, <code>hypnotoad</code> will not work.
That said, it works great on the new <a href="https://blogs.msdn.microsoft.com/wsl/">Windows Subsystem for Linux</a>!</p>

<p>A major usage difference is that hypnotoad (for technical reasons) can&#39;t use command line parameters.
Instead it takes its parameters via configuration.
It also starts on port <code>8080</code> rather than <code>3000</code> to prevent accidentally exposing your development servers unexpectedly.
For now however, let&#39;s set it back, more for an example than any particular reason.</p>

<pre><code class="hljs">{
  secrets =&gt; [
    &#39;<span class="hljs-string">w8S4b+90CWwf</span>&#39;,
    &#39;<span class="hljs-string">yuIB7m88wS07</span>&#39;,
  ],
  hypnotoad =&gt; {
    <span class="hljs-function">listen</span> =&gt; [&#39;<span class="hljs-string">http://*:3000</span>&#39;],
  },

devdata/https_mojolicious.io_blog_2017_12_17_day-17-the-wishlist-app  view on Meta::CPAN


</code></pre>

<p><small>templates/add.html.ep</small></p>

<p>Beyond being interesting to see how the link is used to embed HTML into the page, we also see our first uses of named content buffers via <a href="http://mojolicious.org/perldoc/Mojolicious/Plugin/DefaultHelpers#content_for"><code>content_for</cod...
These add styling that is specific to the page into the <code>&lt;head&gt;</code> tag and inject a panel into the sidebar.
Once this page renders, again before the layout is rendered, the content of that section is available in the <code>sidebar</code> buffer.</p>

<p>The result is a tiny form that contains the data to be stored if the user wants to add it to their wishlist.
Because the resulting main page might be quite large, and I want the user to have easy access to decide if they want to add the item, I&#39;ve placed it in the left hand column.
Perhaps this is bad UX, but for educational purposes, it shows how these buffers can be used.</p>

<p>We also see our first example of <a href="http://mojolicious.org/perldoc/Mojolicious/Plugin/DefaultHelpers#include"><code>include</code></a>.
When called, the renderer immediately renders the template specified and returns the result.</p>

<pre><code class="hljs">&lt;div class=&quot;<span class="hljs-string">panel panel-default</span>&quot;&gt;
  &lt;div class=&quot;<span class="hljs-string">panel-body</span>&quot;&gt;
    &lt;ul class=&quot;<span class="hljs-string">nav nav-pills nav-stacked</span>&quot;&gt;
      %= t li =&gt; link_to &#39;<span class="hljs-string">Log Out</span>&#39; =&gt; &#39;<span class="hljs-string">logout</span>&#39;
    &lt;/ul&gt;



( run in 0.282 second using v1.01-cache-2.11-cpan-de7293f3b23 )