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>&gt;</code> to mean those should be immediate children instead of descendants:</p>

<pre><code>$ perl html.pl &quot;ul.employees &gt; li &gt; img.human&quot;
&lt;img class=&quot;human&quot; id=&quot;fry&quot; src=&quot;...&quot;&gt;
</code></pre>

<p>Now, consider how much work I&#39;ve done there. Almost nothing. I made a DOM object, applied a selector, and I&#39;ve isolated parts of the data. This is the same thing I was doing the hard way before. This way is better and isn&#39;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&#39;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-&gt;new;

my $url = &#39;https://blog.emojipedia.org/new-unicode-9-emojis/&#39;;
my $tx = $ua-&gt;get( $url );

die &quot;That didn&#39;t work!\n&quot; if $tx-&gt;error;

say $tx-&gt;result
    -&gt;dom
    -&gt;find( &#39;ul:not( [class] ) li a&#39; )
    -&gt;map( &#39;text&#39; )
    -&gt;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&#39;s a short table that has ID, name, and score columns. I want to sum all of the scores.</p>

<p>I&#39;m not afraid of doing with this with regexes (emphasize plural there!) but it&#39;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

  -&gt;content_type_like(qr[text/plain])
  -&gt;content_is(&#39;hello world&#39;);
</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-&gt;get_ok(&#39;/text&#39;, form =&gt; { name =&gt; &#39;santa&#39; })
  -&gt;status_is(200)
  -&gt;content_type_like(qr[text/plain])
  -&gt;content_is(&#39;hello santa&#39;);
</code></pre>



( run in 0.255 second using v1.01-cache-2.11-cpan-4e96b696675 )