TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Comparing PHP, Perl, Python and Ruby

382 点作者 charlax大约 13 年前

22 条评论

raymondh大约 13 年前
This is a wonderful recap of the different ways of expressing the same idea in different languages. It is a fine Rosetta Stone.<p>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.<p>Collectively, these capabilities make the language more than just another syntax for expressing the same ideas as other languages.
评论 #3645589 未加载
评论 #3645778 未加载
评论 #3645538 未加载
rll大约 13 年前
Note that anywhere you see array() in that list for PHP 5.4 you can replace it with [ ]'s. So the example that has:<p><pre><code> $a = array(1,2,array(3,4)); </code></pre> becomes:<p><pre><code> $a = [1,2,[3,4]]; </code></pre> Also<p><pre><code> 0b101010 </code></pre> works in 5.4.
评论 #3645552 未加载
verelo大约 13 年前
This is great. Very handy for explaining to others the differences between various languages, and surprisingly enough until you get into the array functions i'm starting to feel like PHP isn't really that messy since reading this!<p>Thanks for sharing, ill pass it on whenever a comparison is needed.
评论 #3645353 未加载
notJim大约 13 年前
This is a really great effort, but I noticed a handful of areas where the PHP example was off. I wonder if people with more experience with the other languages noticed the same thing for their language.<p>Some examples:<p>Null test:<p><pre><code> $var === null // omitted isset($var) // actually checks if $var exists and is not null (apparently-- I actually didn't know about the not null part) </code></pre> Undefined variable access: it's a notice. Many people consider it best practice in PHP to treat notices as error, because things like this are notices.<p>Add time duration<p><pre><code> strtotime('+10 minutes') is more typical than the objects </code></pre> Array out of bounds behavior again issues a notice.
评论 #3647413 未加载
ars大约 13 年前
Previous discussion: <a href="http://news.ycombinator.com/item?id=2882070" rel="nofollow">http://news.ycombinator.com/item?id=2882070</a> with lots of bug reports. I wonder if they incorporated all the changes suggested.
jablan大约 13 年前
"Passing by reference" section is wrong about Ruby (not sure about Python). Ruby passes everything by reference, it's only that integers are immutable so that the function can't alter them. Strings, though, are mutable:<p><pre><code> def foo s s.concat 'foo' end a = 'bar' foo a a # =&#62; "barfoo"</code></pre>
评论 #3648226 未加载
评论 #3647780 未加载
评论 #3647284 未加载
评论 #3647586 未加载
tikhonj大约 13 年前
Here's a simpler comparison with <i>way</i> more langauges: <a href="http://rigaux.org/language-study/syntax-across-languages/Strng.html#8" rel="nofollow">http://rigaux.org/language-study/syntax-across-languages/Str...</a>
citizenkeys大约 13 年前
sed begat awk.<p>awk begat perl.<p>perl begat python and ruby.<p>python will get you a job at google.<p>ruby will get you a job at twitter.<p>php will get you a job at facebook.
评论 #3645871 未加载
评论 #3645874 未加载
评论 #3645837 未加载
评论 #3646294 未加载
smsm42大约 13 年前
Unfortunately tests page for PHP does not mention PHPUnit: <a href="http://www.phpunit.de/manual/3.6/en/index.html" rel="nofollow">http://www.phpunit.de/manual/3.6/en/index.html</a> which is the best solution for testing in PHP. Neither it mentions xdebug for debugging and xhprof for profiling.
laichzeit0大约 13 年前
Perl's treatment of regex still trumps all. It's so pleasant to use in so many ways. It's for this reason alone I will probably never bother "switching" to Python or Ruby.
评论 #3649343 未加载
alberth大约 13 年前
Wish Lua was added to the list.<p>Edit: Found Lua compared on the same site here: <a href="http://hyperpolyglot.org/embeddable" rel="nofollow">http://hyperpolyglot.org/embeddable</a>
kirinan大约 13 年前
Very good comparison between all the languages, and like other people said: a nice Rosetta stone. With that said, it would be very useful to add a comparison of the database layer syntax as that it is, at least for a me, a common task in all of the scripting languages. I also agree with the comments about adding the features that are unique to the languages, as that would be extremely helpful.
评论 #3648679 未加载
methoddk大约 13 年前
Bookmarked and forever using this to explain the differences between these. Thanks for the awesome resource!
devy大约 13 年前
Even though there is no native "switch" statement in Python, the same control flow structure can be done with a dict construct:<p><a href="http://simonwillison.net/2004/may/7/switch/" rel="nofollow">http://simonwillison.net/2004/may/7/switch/</a>
评论 #3645591 未加载
powersurge360大约 13 年前
PHP repl is actually php -a
评论 #3648805 未加载
alexm920大约 13 年前
Wonderful reference for comparison! I admittedly haven't had time to pursue the whole table, but wanted to point out that there is indeed an increment and decrement in python:<p>x += 1<p>x -= 1
评论 #3646017 未加载
评论 #3646958 未加载
Arabian大约 13 年前
The Perl described in this article is COMPLETE SHIT. He calls deprecated and improper libs for some functions, unnecessary libs in others, assumes improper or unoptimal syntax in other examples. The list goes on. Learn languages before releasing articles on them.<p>He never even uses Perl's foreach in equivalency examples!! WTF.
评论 #3646893 未加载
joewadcan大约 13 年前
Very helpful!! Like a quick &#38; easy Rosetta stone
youlost_thegame大约 13 年前
Very, very thorough. A great resource, thanks!
评论 #3647071 未加载
评论 #3645384 未加载
snampall大约 13 年前
Nice. Useful and handy.
berntb大约 13 年前
A very useful page if you know one or two of those languages and need to do something in another one.<p>A suggestion: For people doing some work in Perl, a review of the OO syntax might be critical. Both Moose and the old way.<p>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.<p>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.
rwmj大约 13 年前
[I wonder if this is a personal watershed for Hacker News?]<p>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.<p>"Hyperpolyglot" -- excess in many tongues -- I don't think so.<p>Learn at least some static languages, assembler and some functional languages, and <i>then</i> post something interesting on HN.<p>Well, I have plenty of karma to waste on noobs.
评论 #3645618 未加载
评论 #3645704 未加载