Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Ruby: An Abbot breaks silencewind

by John M. Dlugosz (Monsignor)
on Jun 13, 2001 at 00:12 UTC ( #87922=note: print w/replies, xml ) Need Help??

in reply to Ruby: An Abbot breaks silencewind

For grins, I tried converting this example from the reference from Ruby to Perl.
def fibUpTo(max) i1, i2 = 1, 1 # parallel assignment while i1 <= max yield i1 i1, i2 = i2, i1+i2 end end fibUpTo(1000) { |f| print f, " " }
After all, the oft-touted ability to pass blocks as parameters is something Perl does, too. With the prototype syntax, it should be just as simple, right?

Well, this brings me to my own Meditation. My first try didn't take. Browsing the perlsub page, I find, “An & requires an anonymous subroutine, which, if passed as the first argument, does not require the sub keyword or a subsequent comma.” So, I need to reverse the order of the arguments. Passing a sub last would require a sub keyword in the call. Why is this so?

Meanwhile, it doesn't work for methods, so you can't really use the prototypes to give rise to this syntax for general-purpose iterators (that are part of a collection).

use strict; use warnings; sub fib (&$) { my ($action,$max)= @_; my ($i1, $i2)= (1,1); while ($i1 < $max) { $action->($i1); ($i1,$i2) = ($i2, $i1+$i2); } } fib {print "$_[0] "} 1000;

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2023-12-11 11:51 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (41 votes). Check out past polls.