David Masover
6/4/2008 7:48:00 PM
On Wednesday 04 June 2008 12:20:37 Star Cross wrote:
> Lesson: You can make Ruby every bit as messy as Perl if you want to.
Very true. But can you make Perl as pretty as Ruby?
Every language can be made messy. Not every language can be made clean.
> Perl programmers have got used to being able to tell what they're
> looking at at a glance. $scalar, @array, %hash are all great ways of
> being able to figure out what sort of data you're working with at the
> moment.
Until you start working with objects. Then everything's a scalar:
$myobject->{some_value}
$myobject->method_a()->method_b()
At this point, the only advantage of $ is that it's easier to tell that this
is a variable, and not a command. This distinction isn't as useful in Ruby, I
think, because of how easy it is to use accessors -- at which point, you
don't need to know if it's a variable or a method.
> Ruby doesn't use this convention, and boy did I miss it. The @ character
> makes an appearance to signify a class variable, but that's it.
Actually, there's also $ for globals, and captialization for Constants.
> The fact
> that hashes and arrays are both indexed using square brackets ([])
> contributes to the confusion.
In Perl, I often fell into the trap of naming several variables the same
way -- I would have two or more of:
@foo
$foo
%foo
So, while it's not quite as concise, if there's ever confusion, I will name
things appropriately.
There's also the fact that I can't remember the last time I accessed an array
with the [] notation. Most often, it's through things like <<, each (and
other iterators like inject), and +.
While I'm at it, one thing I do miss from Perl is variable declarations being
enforced with 'use strict' -- in Ruby, the lack of this means that
misspelling a variable name could lead to a much subtler error than "requires
explicit package name".
> There is no CRAN, and there are no Ruby Monks. That's not to say that
> Ruby doesn't have it's own module system (rubygems.org), but it's
> nowhere near as robust as CPAN. Likewise, I managed to find some folks
> to answer questions I had in irc, but Perl monks it is not.
It's worth mentioning that their respective infrastructures (rubygems vs CPAN)
are at least comparable, technically. As far as I know, Python's equivalent
isn't.
And it's not often these days that I can't find something I need as a gem, and
it exists as a CPAN module. Maybe that reflects my own inexperience, though.
It's also a fundamentally different community, I think -- it exists largely in
blogs. When I had a question about how to do something in Perl, I'd most
often look on CPAN and find it already done, with good examples in the
documentation. When I have a question about how to do something in Ruby, I
use Google, and almost always land on someone's blog.
Good post, though. I, too, knew Perl long before I knew Ruby, and I drift back
and forth over time.