Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: coderef for 1 .. 2?

by Anonymous Monk
on Nov 02, 2017 at 01:55 UTC ( #1202565=note: print w/replies, xml ) Need Help??

in reply to coderef for 1 .. 2?

I am fairly certain that the Perldoc is in error ... I find no good reason for what is basically an event-driven routine to call itself upon completion of its work. I would suggest raising that specific point with the module's author. The documentation may by-now be out of date, and not yet corrected.

Replies are listed 'Best First'.
Re^2: coderef for 1 .. 2?
by ronstudio (Novice) on Nov 02, 2017 at 03:17 UTC
    Thanks kcott and everyone for the time and energy involved with this. I feel obligated that I should go further to ask the module author for help if he can explain it further on the example. The mentioned 2 lines
    my $end = $delay->begin; ... $end->();
    are related to IOLoop. From the documentation, each $delay->begin would increment the event counter and the returned code reference can be used as a callback. The callback needs to be executed when the event has completed to decrement the event counter again. However, I just cannot understand the recursion in this case failed to continue calling itself without the for loop.

    The following is the question that I raised in the issue report:
      Hi Ken,

      Many thanks for your help again.
      I think I have finally got it!

      The code example is basically the same example as this AnyEvent example from Perl Maven:

      The "condvar" in AnyEvent is basically the same "delay" in Mojo. I find it easier to understand in the context of AnyEvent that condvar "condition variable" represent a condition that must become true.

      You'll notice the sequence and logic of the codes in both Perl Maven and Mojo cookbook are the same.
      I am wondering if it is possible for the event-loop to finish (and exit) before it can start calling the inner function.

      So as what you have found, the problem is related to the codes for loop control:
      ### my $end = $delay->begin; ... ### $end->();
      I notice if I change the sequence of the $end->(); as following:
      $fetch->(); $end->();
      Instead of
      $end->(); $fetch->();
      The code will now process all the URLs one by one when it is only $fetch->().
      And if I run it with for-loop like $fetch->() for 1 .. 2, it looks like processing 2 URLs in 1 go.
      The code behaving much more like the original intended.

      The problem with the original code is that if you are calling it 1 time only, at the time reaching the end of the event loop the condition may have satisfied and exit the program before it can call another function itself.(my guess)
      (I hope the above phase are correct in describing the issue. This event loop is too new for me)
        Thanks again for everyone's help.

        I have posted the question into the module mailing support:!topic/mojolicious/HftNVDTfM74

        I think my theory is correct that the original code is having the issue that the loop has finished before it can call the inner function itself.

        Thanks everyone!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1202565]
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-05-25 06:09 GMT
Find Nodes?
    Voting Booth?