Acme-CPANModulesBundle-Import-MojoliciousAdvent-2017

 view release on metacpan or  search on metacpan

devdata/https_mojolicious.io_blog_2017_12_05_day-5-your-apps-built-in-commands  view on Meta::CPAN

<pre><code>$ perl santa.pl routes
/toy/:toy_name  GET  &quot;toy&quot;
/meet/:name     GET  &quot;staff&quot;
/               GET  &quot;home&quot;
</code></pre>

<p>This shows you the three routes that were defined.
It shows the paths for each route including their placeholders, that all three handle GET, and their route name.
Using this output is especially helpful when using named routes, as we learned in that post; all the information you need is right in that table!</p>

<p>We can go a little deeper and ask for verbose output by adding a flag</p>

<pre><code>$ perl santa.pl routes -v
/toy/:toy_name  ....  GET  &quot;toy&quot;    ^\/toy/([^/.]+)/?(?:\.([^/]+))?$
/meet/:name     ....  GET  &quot;staff&quot;  ^\/meet/([^/.]+)/?(?:\.([^/]+))?$
/               ....  GET  &quot;home&quot;   ^/?(?:\.([^/]+))?$
</code></pre>

<p>This output includes all the same stuff as before but this time it also adds a few extra items.
Certain routes are more complex, and while all these were simple and so no flags are shown, if one were an <code>under</code> route or a <code>websocket</code> it would be noted where the <code>....</code> are.
Finally it includes a pattern that is what is actually matched by the router.
This can be helpful sometimes when debugging why certain requests match (or more likely don&#39;t match) a certain route.
Note that the router checks each route in order top to bottom, the first to match is what is used.</p>

<h3>The get Command</h3>

<p>Now we&#39;re getting to the fun stuff!</p>

<p>Mojolicious comes with a <a href="http://mojolicious.org/perldoc/Mojo/UserAgent">user agent</a> and lots of post-processing power, including <a href="http://mojolicious.org/perldoc/Mojo/DOM">HTML/XML</a> and <a href="http://mojolicious.org/perldoc...
This command exposes those features together on the command line, like a smart version of cURL or wget.</p>

devdata/https_mojolicious.io_blog_2017_12_05_day-5-your-apps-built-in-commands  view on Meta::CPAN

The <a href="http://mojolicious.org/perldoc/Mojolicious/Command/eval"><code>eval</code></a> command.
This command has the magic power to run one-off commands using your application!
The application is available as <code>app</code> in your one-liner.</p>

<p>So say you can&#39;t figure out what is wrong with your configuration, just ask it to dump what it thinks its configuration is</p>

<pre><code>perl myapp.pl eval -v &#39;app-&gt;home&#39;
perl myapp.pl eval -V &#39;app-&gt;config&#39;
</code></pre>

<p>The <code>-v</code> flag prints the string result of the last statement to STDOUT, the <code>-V</code> flag does the same but for data structures.
Maybe you want to see why it can&#39;t find your templates.</p>

<pre><code>perl myapp.pl eval -V &#39;app-&gt;renderer-&gt;paths&#39;
</code></pre>

<p>This is especially helpful once you have database interactions setup via some model layer.
If you want to see the result for some query, just check.</p>

<pre><code>perl myapp.pl eval -V &#39;app-&gt;model-&gt;users-&gt;find({name =&gt; &quot;Joel&quot;})&#39;
</code></pre>

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

Like many of the major Perl object frameworks, <a href="https://metacpan.org/pod/Moose">Moose</a> and <a href="https://metacpan.org/pod/Moo">Moo</a> included, Mojo::Base feels these are important enough that it imports them for you.
Unlike those others, it goes further.</p>

<p>Since the modern web is trending towards UTF-8 encoding, the <a href="https://metacpan.org/pod/utf8">utf8</a> pragma is loaded; this enables you to use UTF-8 encoded characters right in your script.
Mojolicious does much of your web-facing encoding for you so this <strong>almost</strong> means you don&#39;t have to think about character encoding at all!</p>

<p>And because Mojolicious itself requires Perl 5.10, it also enables all of the <a href="https://metacpan.org/pod/feature">features</a> that came with that version.
This includes <a href="https://www.effectiveperlprogramming.com/2009/12/perl-5-10-new-features/">handy functionality</a> like the <code>state</code> and <code>say</code> keywords as well as the defined-or operator <code>//</code>.
Finally it imports IO::Handle so that all of your handles behave as objects (if you don&#39;t know what that means or why, don&#39;t worry about it).</p>

<p>If this is the only thing you want from Mojo::Base, perhaps in a script or a test file, all you do is pass the <code>-strict</code> flag</p>

<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_12_day-12-more-than-a-base-class  view on Meta::CPAN

Therefore the primary feature of Mojo::Base classes is speed!</p>

<p>The object system is spartan even when compared to Moo.
You get a hash-based object with declarative read/write lazy accessors and a constructor.
When considering Moose vs Moo, you trade lesser-used features for a noticable performance gain.
Likewise when you consider Mojo::Base vs Moo, you strip down futher, this time to the bare essentials, but again get a performance gain.</p>

<p>Now if you need more functionality, you are more than welcome to use Moo or other object systems in your applications (though the Mojo internals will of course continue to use Mojo::Base).
That said, much of the real world usage of Moo is very similar to Mojo::Base: lazy accessor generation; this might be all you need.</p>

<p>To declare a new class with Mojo::Base, one with no other parent classes, you use the <code>-base</code> flag.</p>

<pre><code>package My::Class;
use Mojo::Base -base;
</code></pre>

<p>This adds the <code>has</code> keyword to your package which, as we will see soon, declares the class&#39;s attributes.
This will cause your new class to inherit from Mojo::Base, meaning it will get the methods from Mojo::Base as well, which you will also see.
Of course, the module also acquires the pragmas and functionality listed above and may add <code>-signatures</code> if desired.</p>

<p>If you want your class to derive from some other parent class, you can pass that name rather than <code>-base</code>.</p>

devdata/https_mojolicious.io_blog_2017_12_13_day-13-more-about-roles  view on Meta::CPAN

              </section>
              <section id="section-2">
                  <p>This is not to say that roles couldn&#39;t be or weren&#39;t used in Mojolicious before then, only that Mojo::Base didn&#39;t include any special handling to make it user-friendly.
Prior to the functionality being formally available from Mojo::Base, a few roles were available for Mojo stack classes on CPAN.
To my knowledge they all used Role::Tiny, but they had to roll their own composition mechanisms, presenting some barrier to use.</p>

<h2>Creating Roles</h2>

<p>A role itself looks like a class at first glance.
It has a package name and probably has methods.
Rather than using the <code>-base</code> flag or specifying a parent class, a role uses the <code>-role</code> flag.
Once again, an optional <code>-signatures</code> flag is allowed too.</p>

<p>Let&#39;s look at a (contrived) example.
Say we want to make a role that finds all the tags matching elements in a <a href="http://mojolicious.org/perldoc/Mojo/DOM">Mojo::DOM</a> based on a selector stored in an attribute.</p>

<pre><code>package Mojo::DOM::Role::FindCustom;
use Mojo::Base -role;

requires &#39;find&#39;;

has custom_selector =&gt; &#39;a&#39;;



( run in 6.390 seconds using v1.01-cache-2.11-cpan-94b05bcf43c )