Acme-CPANModulesBundle-Import-MojoliciousAdvent-2018
view release on metacpan or search on metacpan
devdata/https_mojolicious.io_blog_2018_12_05_compound-selectors_ view on Meta::CPAN
<p>If nothing should be between those tags. I can connect the selector with <code>></code> to mean those should be immediate children instead of descendants:</p>
<pre><code>$ perl html.pl "ul.employees > li > img.human"
<img class="human" id="fry" src="...">
</code></pre>
<p>Now, consider how much work I've done there. Almost nothing. I made a DOM object, applied a selector, and I've isolated parts of the data. This is the same thing I was doing the hard way before. This way is better and isn't more work. ...
<h2>How about those new emojis?</h2>
<p>While writing about the <a href="https://www.effectiveperlprogramming.com/2018/08/find-the-new-emojis-in-perls-unicode-support/">Unicode 9 updates in Perl v5.26</a>, I wondered what I could show that might be interesting. How about figuring out wh...
<p>My first attempt simply trawled through every character and compared the various Unicode properties to see which code numbers changed from <code>Unassigned</code> to <code>Present_In</code>. That was fine, but then I found that someone was already...
<p>I won't explain everything in this program. Trust me that it uses <a href="https://mojolicious.org/perldoc/Mojo/UserAgent">Mojo::UserAgent</a> to fetch the data, extracts the DOM, and finds the text I want by using the compound selector <code>...
<pre><code>use v5.28;
use utf8;
use strict;
use warnings;
use open qw(:std :utf8);
use charnames qw();
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
my $url = 'https://blog.emojipedia.org/new-unicode-9-emojis/';
my $tx = $ua->get( $url );
die "That didn't work!\n" if $tx->error;
say $tx->result
->dom
->find( 'ul:not( [class] ) li a' )
->map( 'text' )
->map( sub {
my $c = substr $_, 0, 1;
devdata/https_mojolicious.io_blog_2018_12_05_compound-selectors_ view on Meta::CPAN
<pre><code>ðº (U+1F57A) MAN DANCING
ð¤ (U+1F5A4) BLACK HEART
ð (U+1F6D1) OCTAGONAL SIGN
ð (U+1F6D2) SHOPPING TROLLEY
ð´ (U+1F6F4) SCOOTER
ðµ (U+1F6F5) MOTOR SCOOTER
ð¶ (U+1F6F6) CANOE
</code></pre>
<p>I used the same program to find <a href="https://www.effectiveperlprogramming.com/2018/08/use-unicode-10-in-perl-v5-28/">the Unicode 10 updates in v5.28</a> too.</p>
<h2>Extracting columns from a table</h2>
<p>Not impressed yet? How about slicing a table with CSS Selectors? Here's a short table that has ID, name, and score columns. I want to sum all of the scores.</p>
<p>I'm not afraid of doing with this with regexes (emphasize plural there!) but it's easier with <a href="https://mojolicious.org/perldoc/Mojo/DOM">Mojo::DOM</a>. The compound selector finds the table by its class, selects each row, and addre...
<pre><code>use v5.26;
use utf8;
use strict;
devdata/https_mojolicious.io_blog_2018_12_20_testing-dancer_ view on Meta::CPAN
->content_type_like(qr[text/plain])
->content_is('hello world');
</code></pre>
<p>Each of the above method calls is a test.
The first, <code>get_ok</code>, builds a transaction and requests the resource.
Since the url is relative, it is handled by the app (if we wanted we could request and web resource too using a fully qualified url).
The transaction is stored in the tester object (<code>$t</code>) and all following tests will check it until it is replaced by the next request.</p>
<p>The remaining tests are reasonably self-explanatory, we check that the response status was 200, that we got a content type header that we expected and that its content is as we expect.
The content has already been utf-8 decoded, and the script has implicitly <code>use utf8</code>, so if you expected unicode, you can compare them easily.
The tests return the tester object so chaining is possible, making for visually clean sets of tests.</p>
<p>The next test is similar but this one uses the standard <a href="https://mojolicious.org/perldoc/Mojo/UserAgent">Mojo::UserAgent</a> style request generation to build a query string naming Santa for our greeting.
The tests are all the same except of course that it checks that the content greets Santa.</p>
<pre><code>$t->get_ok('/text', form => { name => 'santa' })
->status_is(200)
->content_type_like(qr[text/plain])
->content_is('hello santa');
</code></pre>
( run in 0.255 second using v1.01-cache-2.11-cpan-4e96b696675 )