Dear All,

I have been learning how to write non-blocking http request.
While following the example from Mojo::UserAgent:
http://mojolicious.org/perldoc/Mojolicious/Guides/Cookbook#Non-blocking

I do not understand the meaning of the following:
$fetch->() for 1 .. 2;
The following is the complete example:
use Mojo::UserAgent; use Mojo::IOLoop; my @urls = ( 'mojolicious.org/perldoc/Mojo/DOM', 'mojolicious.org/perldoc/Mojo', 'mojolicious.org/perldoc/Mojo/File', 'mojolicious.org/perldoc/Mojo/U +RL' ); my $ua = Mojo::UserAgent->new(max_redirects => 5); $ua->transactor->name('MyParallelCrawler 1.0'); my $delay = Mojo::IOLoop->delay; my $fetch; $fetch = sub { return unless my $url = shift @urls; my $end = $delay->begin; $ua->get($url => sub { my ($ua, $tx) = @_; say "$url: ", $tx->result->dom->at('title')->text; $end->(); $fetch->(); }); }; #Process two requests at a time $fetch->() for 1 .. 2; $delay->wait;

I have tried the following:
1) Removing the "for 1 .. 2" part.
i.e. $fetch->();
the result is the http request will always get the first link only.

2) Increase the end point, say "for 1 .. 4"
Based on the comment from the provided example, I believe this will result in processing 4 requests at a time.
Could I ask your help in explaining the meaning of the above syntax?
Does those 1, 2 become the argument passing into the function? (but $fetch is not reading @_, so this shouldn't be the case)
What exactly does that for loop mean in this context?


Many thanks for your help in sharing your Perl knowledge,
Ronald

In reply to coderef for 1 .. 2? by ronstudio

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.