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
See 'APPLICATION help COMMAND' for more information on a specific command.
</code></pre>
<p>As it says, you can now see the more detailed information on each command by running <code>mojo help COMMAND</code> for one of those commands.</p>
<h2>The Built-In Commands</h2>
<p>Since we've already briefly discussed <a href="http://mojolicious.org/perldoc/Mojolicious/Guides/Cookbook#DEPLOYMENT">deployment</a> I'll skip over the servers this time, including the <a href="http://mojolicious.org/perldoc/Mojolicious/Co...
Similarly I'll skip the <code>inflate</code> command.
In the interest of space, I'll skip the <a href="http://mojolicious.org/perldoc/Mojolicious/Command/test"><code>test</code></a> command that simply runs your application's tests like <a href="https://metacpan.org/pod/prove">prove</a>.
I'll also skip <a href="http://mojolicious.org/perldoc/Mojolicious/Command/cpanify"><code>cpanify</code></a> which lets CPAN authors upload modules to CPAN (I use it all the time).</p>
<h3>The generate Command</h3>
<p>Perhaps the first command you use should be the <a href="http://mojolicious.org/perldoc/Mojolicious/Command/generate"><code>generate</code></a> command.
It lets you generate a new application (or other) project skeleton from templates.</p>
<p>It has a few subcommands, including one for generating each type of app.
To create a <a href="http://mojolicious.org/perldoc/Mojolicious/Command/generate/lite_app">Lite app</a>, pass the name of the script to create</p>
<pre><code>$ mojo generate lite_app myapp.pl
devdata/https_mojolicious.io_blog_2017_12_05_day-5-your-apps-built-in-commands view on Meta::CPAN
To do so we fetch the url (following redirects with <code>-r</code>), then we give it a <a href="http://mojolicious.org/perldoc/Mojo/DOM/CSS">CSS3 selector</a>, and finally extract the text from each found element.</p>
<pre><code>mojo get -r reddit.com/r/perl 'p.title > a.title' text
</code></pre>
<p>How fun is that?!</p>
<ul>
<li>You can POST or PUT or DELETE data.</li>
<li>It handles HTTP basic authentication using <code>username:password@</code> in the URL.</li>
<li>You can submit forms, even with file uploads using the standard <code>@filename</code> syntax.</li>
<li>You can pipe data to the command if you just want to send the raw contents of a file rather than url-encode it.</li>
<li>See lots more examples in the <a href="http://mojolicious.org/perldoc/Mojolicious/Command/get#SYNOPSIS">documentation</a>.</li>
</ul>
<p>But I haven't even touched on its coolest feature yet.
This command also works on your application when you request a relative url.
This is so handy for debugging requests during rapid development; you don't even need a browser!</p>
<pre><code>perl santa.pl get /meet/rudolph 'p' text
</code></pre>
devdata/https_mojolicious.io_blog_2017_12_11_day-11-useragent-content-generators view on Meta::CPAN
encode_json({some => ['json', 'data']})
);
</code></pre>
<p>or a similar example to the above using <code>build_tx</code>.
I think you'll agree that the generator form is much easier to read and "does what you mean".</p>
<p>At the time of this writing, Mojo::UserAgent comes with three <a href="http://mojolicious.org/perldoc/Mojo/UserAgent/Transactor#tx">built-in Content Generators</a>, including the <code>json</code> one as we've already seen.</p>
<p>The <code>form</code> generator creates urlencoded or multipart requests depending on the data passed.
The form generator is, unsurprisingly, useful for submittng forms, often used to login to sites, search for content or upload files.
It is even smart enought to use query parameters for <code>GET</code> and <code>HEAD</code> requests (which cannot take a body), while using body parameters for others.</p>
<p>Finally, the recently-added <code>multipart</code> generator is for building your own generic multipart requests.
Though not common, some APIs allow or even require users to upload multiple files in the same request.</p>
<p>This was the case presented to us by a user not too long ago.
They were interacting with the <a href="https://developers.google.com/drive/v3/web/multipart-upload">Google Drive API</a> that wanted them to upload a file as part of a multipart message with a JSON document attached containing metadata.
The overall request was to be marked at <a href="https://tools.ietf.org/html/rfc2387"><code>multipart/related</code></a> while each part should have its own <code>Content-Type</code>.
Google's documented example is</p>
<pre><code>POST https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart HTTP/1.1
Authorization: Bearer [YOUR_AUTH_TOKEN]
Content-Type: multipart/related; boundary=foo_bar_baz
Content-Length: [NUMBER_OF_BYTES_IN_ENTIRE_REQUEST_BODY]
--foo_bar_baz
Content-Type: application/json; charset=UTF-8
{
"name": "myObject"
}
devdata/https_mojolicious.io_blog_2017_12_11_day-11-useragent-content-generators view on Meta::CPAN
<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> '<span class="hljs-string">encode_json</span>';
<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> = '<span class="hljs-string">https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart</span>';
<span class="hljs-keyword">my</span> <span class="hljs-type">$token</span> = '<span class="hljs-string">XXXXXXXXXX</span>';
<span class="hljs-keyword">my</span> <span class="hljs-type">$file</span> = <span class="hljs-function">Mojo::File</span>->new('<span class="hljs-string">local/path/to/image.jpg</span>');
<span class="hljs-keyword">my</span> <span class="hljs-type">$ua</span> = <span class="hljs-function">Mojo::UserAgent</span>->new;
<span class="hljs-type">$ua</span>-><span class="hljs-type">post</span>(
<span class="hljs-type">$url</span>,
{
Authorization => "<span class="hljs-string">Bearer </span><span class="hljs-type">$token</span>",
'<span class="hljs-string">Content-Type</span>' => '<span class="hljs-string">multipart/related</span>',
},
devdata/https_mojolicious.io_blog_2017_12_19_day-19-make-your-app-installable view on Meta::CPAN
<img alt="Container ship loading at the dock" src="/blog/2017/12/19/day-19-make-your-app-installable/container_ship.jpg">
</div>
<div class="post-content">
<section id="section-1">
<p>Thus far we have always run our applications from the local directory.
That is usually the project root directory and/or the repository checkout.
But did you know that with only a few changes you can make your application installable like other Perl modules?</p>
<p>While, you must do this if you want to upload your application to CPAN, even if you don't intend to do that, it still has benefits.
You can install the application on your personal computer, especially if you want to be able to run the script while in other directories.
Having an installable module also means that you can use a so-called "DarkPAN" and related tools to build yourself a local "CPAN".
If you have multiple Perl modules at your company (or as part of any project) using a DarkPAN can ease integration and deployment immensely!
N.B. there is even a DarkPAN tool written using Mojolicious called <a href="https://metacpan.org/pod/App::opan">opan</a>.</p>
<p>And hey if you needed even more more reason, it cleans up your project root directory somewhat too!</p>
</section>
<section id="section-2">
<h2>The Challenge</h2>
devdata/https_mojolicious.io_blog_2017_12_19_day-19-make-your-app-installable view on Meta::CPAN
<h2>In Conclusion</h2>
<p>I have made some of these changes to the <a href="https://github.com/jberger/Wishlist/compare/blog_post/sqlite_model...blog_post/installable">Wishlist App</a>.
You'll see that it it really isn't much to do.</p>
<p>You also see that I only used as much of these instructions as I needed; you can do the same.
There is no magic in any of what you've seen (other than perhaps File::Share).
If you don't need to think about theming or customizing the environment variable, then don't worry about it.
But if you find yourself in that situation, you'll know you can make such features available to your users.</p>
<p>And hey, if it is a useful application, consider uploading it to CPAN.
Though I warn you, once you start contributing to CPAN it can be addictive!</p>
</section>
<small><p><a href="https://en.wikipedia.org/w/index.php?curid=31630711">Image</a> by Gnovick - Own work, <a href="https://creativecommons.org/licenses/by/3.0/" title="Creative Commons Attribution 3.0">CC BY 3.0</a>.</p>
</small>
<p class="tags">
<span>Tagged in </span>:
<a href="/blog/tag/advent/">advent</a>,
<a href="/blog/tag/wishlist/">wishlist</a>
devdata/https_mojolicious.io_blog_2017_12_21_day-21-virtually-a-lumberjack view on Meta::CPAN
</code></pre>
<p>This script will create 0_START.pgm, add a PGM header to it (which will need
fixing later) and writes the bytes it receives from STDIN into that file in
the correct format.</p>
<p>We have used various bits from the Mojolicious framework to achieve this.
<a href="http://mojolicious.org/perldoc/Mojo/Loader">Mojo::Loader</a> lets us put our PGM
header separate from the rest of the code, just like an inline template for a
Lite app.
Since it is usually used to receive file uploads,
<a href="http://mojolicious.org/perldoc/Mojo/Asset/File">Mojo::Asset::File</a> gives us
even simpler file creation and append mechanics than
<a href="http://mojolicious.org/perldoc/Mojo/File">Mojo::File</a>, for outputting the
re-formatted data input.
We do have to tell Mojolicious not to <em>cleanup</em> the file once it is done with
it, though this is small price to pay!
The heavier price is the frowns of disapproval in IRC for such (ab)use.. :)</p>
<p><a href="http://mojolicious.org/perldoc/Mojo/IOLoop">Mojo::IOLoop</a> and
<a href="http://mojolicious.org/perldoc/Mojo/IOLoop/Stream">Mojo::IOLoop::Stream</a> give