"From the above code example, I believe this is an example of recursion. There is condition to exit (return unless my $url = shift @urls;) and $fetch is calling itself within the sub $fetch."

Yes, that's recursion. An exit stategy is a standard feature; all recursive functions should have this: without it, they will recurse infinitely (or, at least, to the extent system resources or configuration allow).

"... if the range operator has been removed ... Shouldn't the code process 1 request at a time until all the @urls have been processed?"

Yes, that's what I would have expected. I ran a few tests and all URLs are processed with any of these:

$fetch->() for 1 .. 2; # or $fetch->() for 1 .. 3; # or $fetch->() for 1 .. 4; # or $fetch->(); $fetch->();

But only one was processed with these:

$fetch->() for 1 .. 1; # or $fetch->();

I did some investigating. I will point out that I'm in no way a Mojo* expert; in fact, I wrote a very tiny application using Mojolicious::Lite about a month or so ago (and, until today, that was my only exposure to this family of modules).

I added lots of additional code to that cookbook example; mostly printing variable values or indications of where the code had got to. Eventually, I tracked this down to "delay" related code. After commenting out these two lines, a single "$fetch->()" processed all URLs:

### my $end = $delay->begin; ... ### $end->();

I was using Perl 5.26.0 and Mojolicious 7.46. Although the latter had only been installed about a month ago, I noticed that there had been five updates since then and 7.51 is the latest; the Changes file showed a number of delay-related comments, so I attempted to install the latest via cpan (I only got 7.50, but 7.51 is only a little over 24 hours old, so probably hasn't reached my CPAN mirror sites yet). I retested, with and without those two lines commented, and got the same results as before.

I checked the bug reports; none really seemed to address this issue; "Remove finish and error events from Mojo::IOLoop::Delay" looked like it was the only one that was related to delays.

I can't really spend any more time on this. The next place to look would probably be Mojo::IOLoop::delay(). There are others here with far more experience with the Mojo* modules; they may be able to provide much better answers than I have.

In the spoiler below, I've added my code with all the debug statements (and some additional code). You may find it interesting to track the process or for further investigations.

— Ken


In reply to Re^3: coderef for 1 .. 2? by kcott
in thread 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.