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

      &#39;<span class="hljs-string">Content-Type</span>&#39; =&gt; &#39;<span class="hljs-string">image/jpeg</span>&#39;,
      content =&gt; <span class="hljs-type">$file</span>-&gt;<span class="hljs-type">slurp</span>,
    }
  ]
);


</code></pre>

<p>Though you have to form the parts a little more manually (no, generators don&#39;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&#39;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&#39;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&#39;s make the instructions get loaded in, skipping any empty lines:</p>

<pre><code>has instructions =&gt; sub {[
  map {[split / /, $_, 2]}
  grep { length $_ }
  split /\n/, data_section(__PACKAGE__, &#39;instructions&#39;)
]};
</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-&gt;instructions};



( run in 0.727 second using v1.01-cache-2.11-cpan-65fba6d93b7 )