Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl - Ruby mapping?

Tags:

ruby

perl

I got involved in a new project where Perl is a must. I'm coming from a good Ruby foundation and want a quick introduction or mapping between Perl and Ruby as I heard that Perl and Ruby are very close in syntax (know nothing about features).

  1. Do you have any recommendations for me?
  2. What great Perl book do you recommend as an extended reference?
  3. What is the commonly used version of Perl right now?
like image 734
khelll Avatar asked Oct 17 '09 15:10

khelll


2 Answers

I second Nathan's book recs, though I would also mention Beginning Perl. Two bonus features are (1) it's available freely (and legally) online in its first edition (note: this site is timing out right now, and I'm unsure if that's temporary or not) and (2) it covers about as much as Learning Perl and Intermediate Perl combined. A con is that it's at times more elementary that you might want. (Learning Perl goes faster and assumes a bit more - which can be a good thing.)

You might also check out this: To Ruby From Perl from Ruby's website. Just think of it in reverse.

In terms of versions, 5.10.1 is stable, but you will come across a range. Mostly you will find 5.8.x and up, I suspect. (Just as with Ruby 1.9.1 is stable but you will find plenty of places still using 1.8.6 or up.)

Since I'm somewhat going in the opposite direction (I know Perl reasonably well, and I'm using Ruby more and more often), I can mention things that stick out to me:

  1. In Perl, you get automatic conversion between strings and numbers (and you don't need to explicitly ask for a float result by using .to_f or making one item a float).
  2. Semicolons are not optional to end statements in Perl. Similarly parentheses are optional less often in Perl than they are in Ruby. (This gets complex quickly, but for example you must have parentheses for the test in a condition or a while block.)
  3. 0 (string, integer and float), undef and the empty string evaluate as false in boolean tests.
  4. There are no separate booleans true and false.
  5. You distinguish data types with sigils: $foo is a scalar; @foo is an array; %foo is a hash. (Arrays in particular will bug you: they aren't instance variables.)
  6. You need to explicitly scope items in Perl, using the my keyword.
  7. Arrays in Perl are automatically flattened when combined. (This constantly bites me in Ruby.)
  8. Context, context, context. In Perl an enormous amount of what your code actually does depends on understanding what context you're in. Here's a link for a start, but it's a big topic with a lot of nooks and crannies.

(Note that I didn't mention the 1000 pound gorilla in the room. OO is part of what Perl is and can do, but it's not at the center of Perl, as it is in Ruby.)

like image 193
Telemachus Avatar answered Nov 03 '22 01:11

Telemachus


Versions

In The Perl Survey 2007***, the majority of Perl coders used Perl 5.8, with 87% using 5.8.x at least some of the time, and 5.8.8 being the most common single version. A minority used 5.6.x for at least some projects, and a smaller (but occasionally quite vocal) minority uses 5.005. Since that point Perl 5.10 has been released and it's not clear yet what the adoption rate is; likely many businesses are being conservative and running 5.8.8 or 5.8.9, but many of what you might call "prominent hackers" are using 5.10.1 and even sometimes requiring its features.

References

  • Programming Perl is a winner for anyone with previous programming experience who wants to get up to speed on Perl quickly. The current edition is the third, which corresponds (unfortunately) to Perl 5.6.0.
  • The series Learning Perl, Intermediate Perl, Mastering Perl is also recommended; Learning Perl starts off rather slow because it targets beginners, but it eventually covers all of the major features of the language, including some that didn't exist in 2000 when Programming Perl was last revised.
  • Perldocs. Please, don't neglect the perldocs. The master index is at perldoc perl, or you can read online at perldoc.perl.org. These are nearly as good a reference as Programming Perl because in fact a decent portion of that book is drawn from the Perldocs. I would recommend at least a thorough skim of perlsyn, perlop, perlrun, perlvar, perlre, perlobj, perlmod, perluniintro, perlreftut, and perldsc. Reading straight through perlfunc once isn't a bad idea either.
  • Effective Perl Programming is a book I've always recommended for learning how to approach problems with a Perl frame of mind. Its 1998 publication date limits its usefulness a bit but I still think it's worth a read -- and fortunately, brian d foy and Josh McAdams are working on an updated edition, due (I think -- don't trust me too far on this) March 2010. [And the second edition is now here -- brian]
  • Perlmonks is a great resource, especially if you remember to use the search feature. A great many questions have been asked and answered there, and the best answers are indexed for posterity, along with lists of resources such as books and online FAQs.

***: I would love to provide a link to The Perl Survey 2007 but unfortunately the perlsurvey.org domain was allowed to lapse. A copy of the results PDF may be found at github, and the mostly-raw data is on CPAN as Data::PerlSurvey2007.

like image 44
hobbs Avatar answered Nov 03 '22 00:11

hobbs