Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: Wait and retry loop without goto

by einhverfr (Friar)
on May 15, 2015 at 07:39 UTC ( [id://1126747]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Wait and retry loop without goto
in thread Wait and retry loop without goto

I think it seems magical because it isn't really a goto. I see why Perl 6 is changing it to &sub.nextwith(). It would be nicer IMO if it had been given its own keyword like tailcall().

Replies are listed 'Best First'.
Re^4: Wait and retry loop without goto
by Laurent_R (Canon) on May 15, 2015 at 12:19 UTC
    IMHO, it is a form of goto in the sense that the code never returns from that call. The Camel book says the following on this form of goto:
    The goto &NAME form is highly magical and sufficiently removed from the ordinary goto to exempt its users from the opprobrium to which goto users are customarily subjected. It substitutes a call to the named subroutine for the currently running subroutine.

    Having said that, in his book Modern Perl, chromatic also suggest to use a goto to eliminate tail-recursion:

    Perl does not eliminate tail calls automatically, but you can get the same effect by using a special form of the goto builtin. Unlike the form which often produces spaghetti code Named because control flow is as simple and straightforward as a plate of spaghetti., the goto function form replaces the current function call with a call to another function. You may use a function by name or by reference. You can even modify the arguments passed to the replacement function by modifying @_:
    # split the array down and recurse if ($item < $miditem) { @_ = ($item, @array[0 .. $midpoint]); goto &elem_exists; } # split the array up and recurse else { @_ = ($item, @array[$midpoint + 1 .. $#array] ); goto &elem_exists; }
    Sometimes optimizations are ugly, but if the alternative is highly recursive code which runs out of memory, embrace the ugly and rejoice in the practical.

    So, you are right, may be it should have been called something like tailcall(), except that the goto &NAME form can also be used for other things than tail reduction elimination.

    Je suis Charlie.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1126747]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-24 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found