Acme-CPANModulesBundle-Import-MojoliciousAdvent-2017
view release on metacpan or search on metacpan
devdata/https_mojolicious.io_blog_2017_12_11_day-11-useragent-content-generators view on Meta::CPAN
'<span class="hljs-string">Content-Type</span>' => '<span class="hljs-string">image/jpeg</span>',
content => <span class="hljs-type">$file</span>-><span class="hljs-type">slurp</span>,
}
]
);
</code></pre>
<p>Though you have to form the parts a little more manually (no, generators don't call other generators), this is still a much simpler use than building the message manually.
Most notably, the length calculations and all of the boundary handling is done transparently.</p>
<h2>Adding New Content Generators</h2>
<p>So if you are reading this and thinking that Content Generators look great but the type you need isn't available, take heart!
Adding content generators is easy too!
As seen in the <a href="http://mojolicious.org/perldoc/Mojo/UserAgent/Transactor#add_generator">documentation</a> adding a generator is as simple as adding a callback that will build the request.</p>
<p>To motivate this discussion, I'll introduce another module.
At work, I had to use <a href="http://xmlrpc.scripting.com/spec.html">XML-RPC</a> to interact with a remote service.
XML-RPC defines an XML schema for asking the service to call a method, just as you would locally, by method name and with some arguments.
devdata/https_mojolicious.io_blog_2017_12_21_day-21-virtually-a-lumberjack view on Meta::CPAN
The text is what is output to the user.
We also use it to get the name of the file to create.</p>
<p>This could be formatted in a lot more of a structured way (e.g. YAML), but
this works fine for our purpose!</p>
<p>Let's make the instructions get loaded in, skipping any empty lines:</p>
<pre><code>has instructions => sub {[
map {[split / /, $_, 2]}
grep { length $_ }
split /\n/, data_section(__PACKAGE__, 'instructions')
]};
</code></pre>
<p>And add a method which progresses us through the script:</p>
<pre><code>sub instruction_show {
my $self = shift;
my $instruction = shift @{$self->instructions};
( run in 0.727 second using v1.01-cache-2.11-cpan-65fba6d93b7 )