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. :-)
| [reply] [d/l] |
|
|
I definitely will! Thanks and good luck. I guess I took laziness to a new extreme..
| [reply] |
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. | [reply] |
|
|
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.
| [reply] |
|
|
| [reply] |
|
|
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...:-)
| [reply] [d/l] [select] |
|
|
Regarding the perl 6 attributes; there is a perl 5 implementation, too. See Lexical::Attributes.
| [reply] |
|
|
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.)
| [reply] |
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?
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
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...:-)
| [reply] [d/l] [select] |
|
|
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++;
};
| [reply] [d/l] |
|
|
| [reply] |
|
|
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.
| [reply] |
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 :)
| [reply] |
|
|
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
| [reply] |