5 ms·
This is a wonderful recap of the different ways of expressing the same idea in different languages. It is a fine Rosetta Stone. That being said, it would also
by raymondh 15y ago
This is a wonderful recap of the different ways of expressing the same idea in different languages. It is a fine Rosetta Stone.
That being said, it would also be useful to characterize features that are completely unique to a given language. For example, Python has 1) a unique and powerful version of super() for working with multiple inheritance; 2) it has generators (the yield statement) that allows state to be suspended and restarted (and allows data and/or exceptions to be passed back in to the generator); 3) the with-statement for managing contexts and for providing a new way to factor code; 4) easy-to-write class and function decorators; 5) metaclass support that provides full control over the creation of classes; 6) and descriptors for fine-control over attribute/method binding behavior.
Collectively, these capabilities make the language more than just another syntax for expressing the same ideas as other languages.
- squiggly101 15y ago7) The iteration protocol, which allows user-defined classes to participate in all iteration contexts, such as the for loop.
- jrockway 15y agoThat being said, it would also be useful to characterize features that are completely unique to a given language. Perl has all these features. 1) a unique and powerful version of super() for working with multiple inheritance Perl (with Moose) has before, after, around, override, and augment in addition to super. And, it has "Roles" so you don't have to resort to hacks like multiple inheritance to implement mixins or interfaces. 2) it has generators (the yield statement) that allows state to be suspended and restarted (and allows data and/or exceptions to be passed back in to the generator) Perl has the library Coro, which allows multiple Perl and C stacks. With this coroutine and call-with-current-continuation are implemented. 3) the with-statement for managing contexts and for providing a new way to factor code Perl has scope a few implementations of scope guards; Guard and Scope::Guard. It's not quite the same as with, because with's semantics around: with open_file('foo') as f: return f are unclear (f is closed but the object still exists). Perl mixes sub x { my $f = file('foo')->openr; return $f; # $f is still open and ready for reading by the caller } Emulating Python is still possible, however: sub x { my $f = file('foo')->openr; scope_guard { $f->close } return $f; # caller gets a closed file object } 4) easy-to-write class and function decorators This you don't really get, though you do get all of the things I've seen them used for in Python. You can even hack Perl syntax in a lexical scope, but that's evil so I will pretend it's impossible :) 5) metaclass support that provides full control over the creation of classes; 6) and descriptors for fine-control over attribute/method binding behavior Moose. And unlike Python, there are libraries that take advantage of this to provide tiny bits of OO functionality that you can pick and choose from. Look on CPAN for a MooseX:: module, and chances are that the set of libraries you choose to use to augment a class will all work together. The difference between Perl and Python is that Perl's features are implemented as modules, while Python's features are implemented in the core. That means that if you come up with a good idea, you can start using (and sharing) it immediately, while with Python, you have to wait until the next major release.
- neilk 15y agoIt also means you have to stumble upon Moose to start using objects in a reasonably sane way. The average new Perl programmer is not going to go looking for a library that does object-oriented behavior until it's too late. They'll probably have some crazy idea that the language should be doing OO for them. If you were to navigate a largish Perl codebase today, you'd encounter the old-school bless, Moose trickiness, and all kinds of variants on these themes. Extremely clever stuff that Damian Conway would give you a gold star for. As usual Perl gives you everything you want, unless what you wanted was simplicity or standardization.
- berntb 15y ago>>The average new Perl programmer is not going to go looking for a library that does object-oriented behavior until it's too late. Sigh, please don't start language wars by stuff like assuming new programmers sits alone in an ivory tower and don't talk to people, read blogs or browse a new book. (Modern Perl discuss Moose before the old standard, for instance.) Edit: I'm just not going to touch where you assume all Perl teams that write large code bases are too stupid to agree on best practices. (I'll ask this back instead: Why are there so many language wars-lovers in some open source communities...?)
- deleted 15y ago[deleted]
- jrockway 15y agoWhy are there so many language wars-lovers in some open source communities...? Probably the same reason the French don't like the English or the Indians don't like the Pakistanis. The closer two things are to each other, the more passionate people are about the differences. My experience is that all the languages are identical. Python has a culture of cleanliness at the core, but there is a lot missing from the language that makes it unclean in practices. (See: the investment bank I worked where all new apps were written in a C#/C++/Python trialect, to coin a new word.) Perl is a very messy language, but the community has hyper-overreacted and have made a very clean set of "best practices". I haven't done much Ruby, but I can't say anything other than that it looks like a combination of Perl and Python, with a relatively new community around it. These three languages are largely the same thing: they run slow and they're fun to write. Hence all the infighting.
- smsm42 15y agoI'm pretty sure you can do the same generator does with iterators in PHP. Traits in PHP 5.4 probably cover most of use cases where you may want multiple inheritance. Decorators are superior in Python though, no real replacement for it in PHP.