Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Perl/Ruby comparison

by srdst13 (Pilgrim)
on Apr 12, 2005 at 11:07 UTC ( #446928=perlquestion: print w/replies, xml ) Need Help??
srdst13 has asked for the wisdom of the Perl Monks concerning the following question:

I write this with some trepidation, but have overcome enough of it to scribe:

I am simply a user of perl, not a guru. In fact, I have little formal training in computer science, let alone language theory. At two levels, I have been wondering about the comparison of perl and ruby, though. On the first level, ruby looks very much like what perl 6 might hold in store, but this is a question more than a statement. On the second level, I would be interested to hear from folks using both languages about a comparison of the "resources" offered by each--in practice, how well developed are the two languages relative to each other. Perl has CPAN, ruby has its RubyGems (and others?). I have invested a great deal of time in learning perl, including object-oriented coding and I don't plan to give that up. (I have not REALLY found a task that perl could not perform, yet.) I ask out of simple curiosity.

I appreciate any enlightenment from the Monks.

Replies are listed 'Best First'.
Re: Perl/Ruby comparison
by Joost (Canon) on Apr 12, 2005 at 12:34 UTC
    Ruby and perl are quite alike, so I'll pick out a couple of differences that I find interesting.

    One of the most important design decisions in ruby is that everything is an object and practically everything you can do with those objects is implemented as a method call, in fact, a lot of the functionality that in perl is implemented as operators and functions are just method calls in ruby:

    container.each { |element| # do something with element }

    Is more or less equivalent with

    foreach my $element (@container) { # o something with $element }

    Except that every container can decide how to implement its each method, which means you can keep the ruby code exactly the same if you decide to switch from a standard array to another implementation (like requesting records from a database, or reading lines from a filehandle etc etc) *)

    Compared to perl, ruby makes extensive use of polymorphism, and less auto-conversion of objects. For instance, a "+" operator will concatenate strings and add numbers in ruby **) while in perl "+" will convert strings to numbers and add them - this is one of the reasons perl has so many operators: since they force conversions on their operands, you need "+" and ".", "*" and "x" etc. This difference will be even more apparant in perl 6.

    One of the advantages of doing it the way perl does, is that it takes less typing; you almost never have to explicitly convert from strings to numbers or back. One of the advantages of doing it the way ruby does, is that it keeps the basic language simpler (less operators) and if the API is good it can make it easier to inject your own object types into existing code/library calls. Also IMHO ruby's way scales better. See the container.each example above.

    Also, ruby has stable, light-weight multithreading compared to perl. This is, at the moment, my biggest reason for using ruby instead of perl for some programs - you can quite easily run a program with 200+ threads in ruby, while in perl, using 200 threads or forking 200 sub-processes tends to slow things down too much.

    If you want to know more, I suggest you get a good book on ruby; I like the pickaxe book. I have the second edition in paper, but the first edition is also freely available online.

    *) Yes, you can tie an object to an array in perl, but that's not quite the same thing.
    **) Note that in ruby, 1 + "a" is an error.

Re: Perl/Ruby comparison
by Roy Johnson (Monsignor) on Apr 12, 2005 at 12:39 UTC
    There's a comparison here (by someone who wants to get away from Perl; you have to consider the source). His Perl could have been tightened up considerably. You can see many such comparisons if you Google perl ruby comparison or perl vs ruby.

    I was impressed with a different benchmark, posted here in PM some time ago, that showed that Ruby generally has shorter source code for the same problem, with similar performance to Perl. I was further impressed with how quickly Ruby built when I downloaded and installed it. Unfortunately, I haven't had time to get into programming Ruby. It seems to me that Perl, Python, and Ruby all fill roughly the same niche. Preferences are generally based on style.


    Caution: Contents may have been coded under pressure.
      His Perl could have been tightened up considerably.

      Boy, is that a call to action! I can't believe I'm the first in 4 years to have taken it up …. Here's a more-or-less faithful copy:

      use strict; use warnings 'all'; use constant { EMAIL => 17, CONTACTME => 27, SKUTITLE => 34 }; my @records; # Arrays are initialised empty; no need to do it explicit +ly. push @records, [ split "\t" ] for <>; do { tr/"//d for @$_ } for @records; my @contact_records; do { push @contact_records, [ @$_[ SKUTITLE, CONTACTME, EMAIL ] ] if $ +_->[CONTACTME] == 1 } for @records; { local $, = "\t"; local $\ = "\n"; print @$_ for sort { $a->[0] cmp $b->[0] } @contact_records; }

      To me, this task suffers, rather than benefits, from the names, and is much better suited for a Unix-style pipeline; fortunately, Perl is good at those (although I resort to substr to avoid the to-me unsatisfying s///; $_):

      use strict; use warnings 'all'; use constant { EMAIL => 17, CONTACTME => 27, SKUTITLE => 34 }; { local $, = "\t"; local $\ = "\n"; print @$_ for sort { $a->[0] cmp $b->[0] } grep { $_->[1] } map { chomp; [ map { substr $_, 1, -1 } ( split "\t" )[ SKUTITLE, CONTACTME, EMAIL ] ] } <>; }
      I've replaced the test for whether the CONTACTME field == 1 (not eq "1", ugh) with a test for whether it's true, since that's almost certainly what was meant; but it's easy to change it back.

      Finally, note that, since the CONTACTME field is not carrying any useful information, we might as well avoid printing it; so we could define

      sub snip_or_toss { $_[CONTACTME] ? @_[ SKUTITLE, EMAIL ] : () }
      replace the outer map by map { chomp; [ map {...} snip_or_toss split "\t" ] }, and then get rid of the grep.

Re: Perl/Ruby comparison
by perrin (Chancellor) on Apr 12, 2005 at 13:49 UTC
    If you're asking about the size of the user community and the freely available resources, there is no comparison. Despite the frequent blog postings, Ruby's user base remains very small. Perl is one of the most popular languages in the world, and CPAN is very large and useful. Some people may find Ruby's features more interesting, but it's still just getting started in terms of relative usage and the maturity of its CPAN-equivalent.
      If you're asking about the size of the user community and the freely available resources, there is no comparison. Despite the frequent blog postings, Ruby's user base remains very small. Perl is one of the most popular languages in the world, and CPAN is very large and useful. Some people may find Ruby's features more interesting, but it's still just getting started in terms of relative usage and the maturity of its CPAN-equivalent.

      Which isn't to say that Ruby isn't ready to be used in the real world.

      While the Ruby community is smaller than Perl's, it's active and helpful so you can get questions answered and problems solved.

      Ruby itself has been around for a fair number of years and has been beaten up enough to have the rough edges knocked off. It's stable and works well in my experience.

      RubyGems et al aren't as expansive as CPAN, but for a lot of the stuff I do it's reached the stage where the more complicated bits of what I need come off the shelf.

      To be honest the only things I really miss in Ruby are the attention Perl module authors pay to documentation, I spend too much time in Ruby reading code, and Perl's testing framework. TAP and the Test:: modules rock :-)

Re: Perl/Ruby comparison
by tall_man (Parson) on Apr 12, 2005 at 15:48 UTC
    There several things missing in ruby compared to perl that limit my desire to go to that language.

    One thing I miss is the amenities to reduce the amount of quoting the user has to do when using hashes. Compare:

    h = {"one"=>1, "two"=>2, "three"=>3 } h["four"] = 4

    to this in perl:

    %h = (one=>1, two=>2, three=>3); $h{four} = 4;

    You can tell perl was written by a linguist -- there's more interest in making the language expressive, with natural shortcuts, rather than "pure."

    Being able to convert input strings to numbers instantly is another nice shortcut. "Everything's an object" gets in the way of that in ruby.

    The worst thing is the complete lack of a protective mode like "use strict;" as I discussed here. Writing in ruby feels unsafe to me.

    Perhaps I am spoiled by some of the features in perl. If I had started first with ruby I might like it better.

      h = {"one"=>1, "two"=>2, "three"=>3 } h["four"] = 4

      Then again in most Ruby code it would be more idiomatic to use symbols instead of strings as the keys so it would come out as:

      h = {:one=>1, :two=>2, :three=>3 } h[:four] = 4

      which rolls off the fingers quite nicely in my opinion.

      You can tell perl was written by a linguist -- there's more interest in making the language expressive, with natural shortcuts, rather than "pure."

      Then why do I end up typing less when I use Ruby? :-)

      Some things are shorter in Ruby than Perl. Some things are shorter in Perl than in Ruby. I certainly don't find Ruby more verbose than Perl. The opposite if anything.

      Being able to convert input strings to numbers instantly is another nice shortcut. "Everything's an object" gets in the way of that in ruby.

      It's not the everything-is-an-object bit of Ruby that gets in the way. Everything is an object in Perl 6 and it keeps the auto-conversion between numbers and strings.

      It's a design decision on how those objects behave. For me it's six of one and half dozen of the other - neither option makes a huge difference to the way I code.

      The worst thing is the complete lack of a protective mode like "use strict;" as I discussed here. Writing in ruby feels unsafe to me.

      I have to admit that I too thought that this would be a problem, but I've found this a complete non-issue when using Ruby.

Re: Perl/Ruby comparison
by dragonchild (Archbishop) on Apr 12, 2005 at 12:48 UTC
    (I have not REALLY found a task that perl could not perform, yet.)

    Then, why do you worry?

    Yes, Ruby has some very nice features. So does Javascript. I personally like Javascript's OO features better than Perl's, and it treats functions as first-class datatypes (which Perl doesn't). So ... should we all use Javascript now?

Re: Perl/Ruby comparison
by johnnywang (Priest) on Apr 12, 2005 at 17:39 UTC
    I just started playing with Ruby, not knowing enough about it to do any comparison. I was bitten by the "ruby on rails" bug, so just had to try it out. It was nice and simple, at least for the simple example I tried. It's nice to do some comparisons, but that shouldn't really be the base for learning or not learning a language, I think we should all try to learn other languages, at least that helps us to appreciate perl, and stay away from its weaknesses (in my humble opinion: threads, gui, POE is also behind python's Twisted)
Re: Perl/Ruby comparison
by sh1tn (Priest) on Apr 12, 2005 at 11:23 UTC
    What's the difference between lamborghini and the rest cars?
    It's simply a matter of perfection.




        True, unless we speak about performance. Not to mention the nightmare (also known as "write-only code") when it comes to large projects. My personal choice is Python.
      "it can be deal with many ways (in Perl)" - as result - not easy to read existed libraries/modules for newbies, like me :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://446928]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-12-15 17:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (70 votes). Check out past polls.

    Notices?