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
<div class="post-content">
<section id="section-1">
<p>One of the things we know from years of programming is that you should never hard-code anything if you don't have to.
And yet far too many web application hard-code their urls, especially internal ones.
But what if you didn't have to?</p>
<p>Each Mojolicious route has its own <a href="http://mojolicious.org/perldoc/Mojolicious/Guides/Routing#Named-routes">name</a> which can be used to generate urls.
If you don't specify one, one is generated, but you shouldn't rely on that name, give it one that is meaningful and relevant to your purposes.
In lite apps, the name is the last parameter, after any defaults or callbacks.
(In a full app it is an attribute, but we'll talk about those in another post).</p>
<p>Then when you need a url, rather than hard-coding it, use <a href="http://mojolicious.org/perldoc/Mojolicious/Controller#url_for"><code>url_for</code></a> or related functionality to generate a url by name, you can even pass placeholder values if ...
Let's see how it works!</p>
</section>
<section id="section-2">
<h2>The North Pole Homepage</h2>
<p>Santa started out with a simple webapp.
devdata/https_mojolicious.io_blog_2017_12_06_day-6-adding-your-own-commands view on Meta::CPAN
<p>Then there's something I haven't shown you before, a helper.
Helpers are like methods but they are available to the app and the controllers and even to templates.
They always receive a controller, even if they are called on the app.
Helpers are very useful for tying parts of your application together.
In this case we use one to build and return an instance of our model class.
It attaches that required data that we noted earlier.</p>
<p>Moving on, the application now defines a route.
As we'll see later it is attached to the <a href="https://github.com/jberger/MyWeatherApp/blob/master/lib/MyWeatherApp/Controller/Weather.pm">Weather controller class</a> and more specifically its <code>recall</code> action method.
This is much like the action callbacks we saw before, but by keeping it in a separate class the application class is easier to read.</p>
<p>Finally we define the database schema.
This is a format common to the Mojo-flavored database modules, like <a href="http://mojolicious.org/perldoc/Mojo/Pg">Mojo::Pg</a>, <a href="https://metacpan.org/pod/Mojo::mysql">Mojo::mysql</a>, and <a href="https://metacpan.org/pod/Mojo::SQLite">Moj...
Each section is defined with a version number and the word <code>up</code> or <code>down</code>.
When migrating versions, it will apply each change set from the current version (beginning at 0) until the version you request.
If you don't request a version it gets the highest version.</p>
<p>Now that all that is done, we can try it out!</p>
<pre><code>$ perl bin/myweatherapp eval -V 'app->weather->fetch("Chicago")'
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' 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->get('/url' => sub {
my ($ua, $tx) = @_;
...
});
</code></pre>
devdata/https_mojolicious.io_blog_2017_12_12_day-12-more-than-a-base-class view on Meta::CPAN
# attribute that defaults to a new, empty hash reference
has 'data' => sub { {} };
# attribute that uses its existing state to build
has 'double_max' => sub {
my $self = shift;
return 2 * $self->max;
};
</code></pre>
<p>The callbacks are always lazy, meaning if the value of that attribute hasn't been established, either via the constructor or via a setter, then the default is used or the builder is run.</p>
<p>The default constructor (<code>new</code>), inherited from Mojo::Base, takes a hash reference or key-value pairs and uses them as initialization for the defined attributes.</p>
<pre><code>my $obj = My::Class->new(foo => 'bar', max => 10);
my $obj = My::Class->new({foo => 'bar', max => 10}); # same
</code></pre>
<p>Note that there is nothing to prevent you from passing data that isn't for a defined attribute (ie, the constructor isn't <a href="https://metacpan.org/pod/MooX::StrictConstructor">strict</a>).
Nor is there anything that declares a required attribute, though you can easily make one</p>
devdata/https_mojolicious.io_blog_2017_12_14_day-14-you-promised-to-call view on Meta::CPAN
<p>A new feature of <a href="http://mojolicious.org/">Mojolicious</a>, as of <a href="https://metacpan.org/release/SRI/Mojolicious-7.49">7.49</a>, is the implementation of the <a href="https://promisesaplus.com/implementations#in-ot...
</section>
<section id="section-2">
<h2>Background</h2>
<p>"Normal" Perl code runs synchronously: it does each step it is told to, one at a time, and only that. This is also known as "blocking", since the program cannot do anything else.</p>
<p>The essence of a non-blocking code framework is that if you are waiting for something, you can register with the framework what to do when that thing happens. It can then do other processing tasks in the meantime. This means you don't have lot...
<p>Originally this was done just using callbacks, but this lead to what is known as "callback hell": each callback contains the next callback, at an increasing level of indentation. Even harder to keep track of is if the functions are kept ...
<p>Promises are used to easily add processing steps to a transaction: one can keep adding code for what to do "then" - after a previous stage has finished. Best of all, each "callback" is small and separate, with each one placed i...
<p>First let's get web pages, one after the other, synchronously. Obviously, that means the code will block anything else while it's running.</p>
<pre><code># refers to a previously-set-up @urls
sub fetchpages {
while (my $url = shift @urls) {
# Fetch, show title
say $ua->get($url)->result->dom->at('title')->text;
( run in 0.377 second using v1.01-cache-2.11-cpan-9b1e4054eb1 )