Live data from Hacker News

Comparing PHP, Perl, Python and Ruby

hyperpolyglot.org

11–20 of 92 posts

Re: Comparing PHP, Perl, Python and Ruby

#13
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.

Re: Comparing PHP, Perl, Python and Ruby

#14
post #3

Earlier quoted context omitted.

PHP5.4 also has the short syntax array initializer: $array = [1, 2, 3]; Edit: While PHP still has it's fair share of issues, a lot of the same complaints people had 5-8 years ago aren't valid, and it probably has no more or less oddities than any other scripting language at this point.

The problem with PHP is that 99.99% of the code in circulation today isn't the shinier cleaned-up versions. It's PHP4 and PHP 5.0 and it's still chock-a-block with the storied mind-bending nastiness of yore.

not true at all. Maybe if you're looking at old sites, but PHP5's been out for a long time now, and most of the code in circulation is made for 5.2 or higher. No serious MVC framework, for instance, will run on anything less than 5.2.

Re: Comparing PHP, Perl, Python and Ruby

#15

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 susp…

7) The iteration protocol, which allows user-defined classes to participate in all iteration contexts, such as the for loop.

Re: Comparing PHP, Perl, Python and Ruby

#16
A very useful page if you know one or two of those languages and need to do something in another one.

A suggestion: For people doing some work in Perl, a review of the OO syntax might be critical. Both Moose and the old way.

A note, re the repl part: I don't use a repl for Perl, I just write one liners in shell. Afaik, Perl is the best command line tool that exist, it is even a superset of awk.

Edit: For named parameters the page might want to reference one of the CPAN modules doing that. Same goes for exceptions. The use of do to create blocks most everywhere might also be noted in e.g. the ternary operator. Unicode support should probably also be discussed. And so on.

Re: Comparing PHP, Perl, Python and Ruby

#17
[I wonder if this is a personal watershed for Hacker News?]

These languages are essentially the same thing. I often write code in at least 3 of these languages most days, and I simply translate the minor differences between them in my head.

"Hyperpolyglot" -- excess in many tongues -- I don't think so.

Learn at least some static languages, assembler and some functional languages, and then post something interesting on HN.

Well, I have plenty of karma to waste on noobs.

Re: Comparing PHP, Perl, Python and Ruby

#19
post #6

Note that anywhere you see array() in that list for PHP 5.4 you can replace it with [ ]'s. So the example that has: $a = array(1,2,array(3,4)); becomes: $a = [1,2,[3,4]]; Also 0b101010 works in 5.4.

True, but PHP 5.4 doesn't even have a stable release yet & many people haven't even adopted 5.3.

Re: Comparing PHP, Perl, Python and Ruby

#20

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 susp…

That 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.

Post reply on HN