Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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;
—John

In reply to Re: Ruby: An Abbot breaks silencewind by John M. Dlugosz
in thread Ruby: An Abbot breaks silencewind by coreolyn

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2024-04-18 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found