Dear Monks, I noticed a thread on slashdot (Ruby On Rails Showdown with Java Spring/Hibernate), and a brief comparison of ruby over python piqued my interest. I believe perl 6 design has considered ruby and wonder if it considers/implements whatever whiz bang features of ruby on rails there may be (of which I am relatively clueless). Not wanting to start a flamewar.

My understanding is that with perl 6 / parrot, perl will become more extensible and could perhaps add things from other parrot-supported languages.. anyway will perl 6 have things like what ruby has below? In particular can you have code macros to create new languages over perl (like rails). Would such macros be only usable in perl? For that matter I'd also like to know if the basic grammar of perl will be fluid, i.e. if you wanted to be able to include English-like logical statements within perl.

I'm curious about the role Perl will have in the future and if parrot will give it a special role with respect to other languages. It might also have an impact on corporate decisions on whether to use perl or not in the future I suppose.

Following are some snippets that illustrate the capabilities I'm curious about, and unfortunately too lazy / backlogged on my reading to answer myself.

Automatic get/set method creation:

class Foo attr_accessor :bar, :baz, :foo end
Now the class Foo has accessor methods (getter and setter) for the instance vars bar, baz and foo. A total of six methods were created from that one line of code. No need for a 'modern Java IDE' - vi or emacs work fine. You want read-only access? Use attr_reader then only the getter methods will be created

Passing code blocks around as macros

That's what I'm talking about. In Python you can't do something like this:

def my_while(cond) return @ret unless cond @ret = yield retry end my_while i<10 { puts "i: #{i}" j = 0 my_while j<10 { puts " j: #{j}" j+=1 } i+=1 }
my_while looks like a natural extension of the language. I'm sure you could duplicate this function in Python, but it wouldn't look like a natural part of the language.

Miscellaneous claims

As far as Ruby vs. Python features go:
1) code blocks. Ruby lets you pass code blocks around. Sounds pretty dull, eh? But in fact it's what makes it possible to create Domain Specific Languages in Ruby quite easily without needing to create a special parser. In many ways Rails can be thought of as a domain specific language built on Ruby.
2) classes are always open in Ruby(including the Class class). By 'open' I mean you can always add new methods to a class (or even a particular object). Another feature that makes it easy to create DSLs
3) continuations. (Not that Rails makes use of them, but some other Ruby-based web programming frameworks do)
4) Ruby has true lambdas. AFAIK Python's lambdas are pretty limited (limited to one expression?)

Also, can you embed Python in HTML? (seems that the whitespace issue would cause a lot of problems with doing that)

I'm just looking for the practical solutions...

Give RoR a try. You might just find that it'll do everything you need it to do now without needing to wait around for a Python-based clone. What could be more practical?



Thanks, Matt R.

Replies are listed 'Best First'.
Re: Perl 6 and Ruby on Rails
by TimToady (Parson) on Apr 05, 2005 at 07:10 UTC
    Yes, Perl 6 will have all those things, only better. But we're too busy designing and implementing them to do your research for you. Please see dev.perl.org for more on Perl 6, including the answers to every single one of your questions, except for the ones about Python. :-)
      I definitely will! Thanks and good luck. I guess I took laziness to a new extreme..
Re: Perl 6 and Ruby on Rails
by Thilosophy (Curate) on Apr 05, 2005 at 04:04 UTC
    Being a Perl monk, I cannot comment much on Ruby vs Python, but if you like how Ruby On Rails works, you should take a look at some Perl modules that follow the same ideas, such as Maypole, Class:DBI and especially Catalyst.
      Thanks a lot, I haven't used Maypole or Catalyst before. Looks like Catalyst is pretty web-centric, but I'll study it a bit more to see if it might be useable with a different interface (i.e. a web service). I like the auto-discovery, that's neat.
Re: Perl 6 and Ruby on Rails
by rg0now (Chaplain) on Apr 05, 2005 at 11:09 UTC
    Here are the relevant parts of S12 regarding automatic generation of accessors:

    Attributes

    Attributes are stored an an opaque datatype, not in a hash. Not even the class has to care how they're stored, since they're declared much like ordinary variables. Instead of my, use has:

    class Dog is Mammal { has $.tail is rw; has @.legs; has $:brain; ... }
    Public attributes have a secondary sigil of dot, indicating the automatic generation of an accessor method of the same name. Private attributes use a colon to indicate that no public accessor is generated. Some traits are copied to the accessor method. The rw trait causes the generated accessor to be declared rw, making it an lvalue method. The default is a read-only accessor.
    and the class openness stuff:
    Classes are open and non-final by default, but may easily be closed or finalized by an application if nobody issued an explicit compile-time request that the class stay open or non-final.

    Regarding "Passing code blocks around as macros", you have no better choice than to read S4, which pretty much answers your question, though, I am not sure the exact syntax to mimic the behavior you refer to. In my understanding, Perl 6 blocks and subs are just continuations, which opens up some neat generalizations here.

    Regarding your comments on Python, I can not really help you here: I pretty much like Python, but every time I decide to use it for some specific tasks, I always end up with a nice Perl script...:-)

    rg0now

      Thank you *very* much rg0now. I see, the mixed in roles are neat, and the trait verbs look quite powerful and intuitive. Looks like you can get methods from other languages since the PyDict layout is noted. And that smart match ("~~") on meta methods not only looks like a new Japanese smiley but seems like it could be really useful. Since S2 talks about mutability and macros it looks like just about anything would be possible in the future, even a language that looked like English prose. Certainly I'm looking forward to studying more about Perl 6! (And thanks Joost I'll look more at Lexical::Attributes for practice.)
Re: Perl 6 and Ruby on Rails
by rg0now (Chaplain) on Apr 05, 2005 at 13:23 UTC
    Here is my strike at "Passing code blocks around as macros":
    macro statement_control:<my_while>($expr, &whileblock) { while $expr &whileblock(); # do we need while $expr do &whileblock(); ? } my $i = 0; my_while {$i < 10} { say "i: $i"; my $j = 0; my_while {$j < 10} { say "j: $j"; $j++; } $i++; }
    I think you could do that without the macro-magic with simple subs like
    sub my_while(Code &expr, Code &whileblock) { while &expr() &whileblock(); }
    This is because (from S4):
    Every block is a closure. (That is, in the abstract, they're all anonymous subroutines that take a snapshot of their lexical scope.) How any block is invoked and how its results are used is a matter of context, but closures all work the same on the inside.

    Digging around the Perl 6 specs, I begin to see that perhaps S4 holds some of the nicest things in the design of Perl 6. And, quite frankly, the more I read the more I feel how much I miss a working Perl 6 compiler...

    Update: hey, we can, with a slightly more convoluted syntax, do this with Perl 5 too:

    use strict; use warnings; sub my_while(&&){ my($expr, $whileblock) = @_; while(&$expr()){ &$whileblock(); } } my $i = 0; my_while sub {$i < 10}, sub { print "i: $i\n"; my $j = 0; my_while sub {$j < 10}, sub { print "j: $j\n"; $j++; }; $i++; };
    Then, what's the great deal?

    rg0now

      the more I read the more I feel how much I miss a working Perl 6 compiler

      What's pugs? Chopped liver? :-)

        Now guess what! I managed to make the damned thing work in Pugs! Here is the slightly modified version that compiles and runs with current Pugs:
        use v6; sub my_while(Code $expr, Code $whileblock) { while ($expr()) { $whileblock() }; } my $i = 0; my_while {$i < 10}, { say "i: $i"; my $j = 0; my_while {$j < 10}, { say "j: $j"; $j++; }; $i++; };
        What really surprized me was the fact that Pugs appears to do the right thing on closing closures, which I did not think should work right now. Also, do not really expect Pugs to verify whether my_while is called with actual blocks or not, because it does not work. Moreover, it is astonishingly slow. But I keep on loving it...:-)

        rg0now

      It doesn't need to be quite as bad as that... though still, bad enough:
      my_while { $i < 10 } sub { print "i: $i\n"; my $j = 0; my_while { $j < 10 } sub { print "j: $j\n"; $j++; }; $i++; };
      hey, we can, with a slightly mlre convoluted syntax, do this with Perl 5 too:

      That's how Error works, and it has memory-leak issues. :-)

      Hmm. Definitely I will do some more reading. Thanks for the experimentation.

      I was thinking about a way to state business rules for a system I'm building now, but to be honest I always wondered when we would get to the point that programming looked more like talking to the "Smart Girl" spaceship Gay Deceiver in Robert A. Heinlein's The Number of the Beast. Between Cyc and Perl6 we seem to be getting closer..

      "Program, Gay. Add running news retrieval. Area, Arizona Strip north of Grand Canyon plus Utah . Persons: all persons listed in current running news retrieval programs plus rangers, Federal rangers, forest rangers, park rangers, state rangers. End of added program."

      The above story uses a verbally programmed autopilot a bit smarter than what we have now, but English constructs like verb-object-prepositional_phrase-conditional_clause might be possible with Perl 6.. Anyway thank you very much and I will certainly do some more reading on it.

Re: Perl 6 and Ruby on Rails
by Akhasha (Scribe) on Apr 06, 2005 at 07:59 UTC

    As far as Ruby vs. Python features go:

    1) code blocks. Ruby lets you pass code blocks around. Sounds pretty dull, eh? But in fact it's what makes it possible to create Domain Specific Languages in Ruby quite easily without needing to create a special parser. In many ways Rails can be thought of as a domain specific language built on Ruby.

    I'm not sure how this is different to passing function-pointers/coderefs/callables around. Certainly the flexbility of Python, with extensible and substitutable types and operators, could lead one to implement different semantics for most expressions than those prepackaged. Harder to get around the whitespace-is-syntax feature though. If someone can expand on the difference w.r.t. Ruby please do.

    2) classes are always open in Ruby(including the Class class). By 'open' I mean you can always add new methods to a class (or even a particular object). Another feature that makes it easy to create DSLs

    AFAIK this is also the case for Python and Perl. In Python you can think of every object (and classes are objects) as a hash, with attributes you can diddle at your leisure (and peril?). Perl's blessed references and package symbol tables might be a little less flexible, in that I think adding a method to an object would involve putting it in the package symbol table, thus adding it to every object of that class. (please correct me if I'm wrong on this)

    3) continuations. (Not that Rails makes use of them, but some other Ruby-based web programming frameworks do)

    Python (2.4) has generators that 'yield' values to the caller and when called again continue from the line after the last yield.

    4) Ruby has true lambdas. AFAIK Python's lambdas are pretty limited (limited to one expression?)

    Limited to a list of expressions, no statements allowed. IMHO this is a notable shortcoming of Python, its also easier to create anonymous subs in Perl. Python requires you to take a reference of a named function, or write a named function that returns a code reference. But that's not a show-stopper.

    Thanks for your post, I'm often reading this or that about different languages and enjoy comparing them. Perl is my job, Python is a hobby and Ruby a mystery.

    To answer your question about embedding Python in HTML, its not so bad. Breaking up long lines of HTML+Embedded code is a good idea. Check out Poor Man's Zope for instance.

    Update: I reread the node and see what is meant by passing blocks of code around. Sorta :)

      Great, thanks for the response. Just to clarify I am not advocating (nor do I really know anything about) Ruby on Rails, nor am I a Ruby programmer. Most everything in the post between "..to answer myself" and "Thanks, Matt" is verbatim snipped from slashdot posts in the referenced thread.

      As I reread a few times my own post I realize the thrust is: Can you create "Domain Specific Languages" easily in Perl 6, and can they look like a natural language (i.e. a minimum of punctuation, use of space-separated words, etc.). I suspect yes though there certainly will be some caveats, for example the "has" reserved word in Perl 6 looks like Class::DBI's has_a() or has_many(). So a nasty semantic mess is possible.

      classes are always open

      I think I saw the same mentioned in a Perl 6 Synopsis.

      thus adding it to every object of that class

      However this I think is contradicted by (was it S4?) mentioned traits and how you can add methods to an object from another object. The converse of that would seem to be the smart match where the tree of inheritance is walked up to find a module that can/handles a method on your object. Sorry I'm newbie to 6.

      As for embedding Python in HTML, sorry that was part of a copied snippet not a wish of mine for sure. I once had to customize a template for an Infoseek installation and it was just that.. it struck me as exceedingly ugly and painful, but maybe because they just used too much of it. This was some years ago..

      Thanks, Matt