|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: To Ruby From Perl |
| 4 | +--- |
| 5 | + |
| 6 | +Perl is awesome. Perl’s docs are awesome. The Perl community is … |
| 7 | +awesome. However, the language is fairly large and arguably complex. For |
| 8 | +those Perlers who long for a simpler time, a more orthogonal language, |
| 9 | +and elegant OO features built-in from the beginning, Ruby may be for |
| 10 | +you. |
| 11 | + |
| 12 | +### Similarities |
| 13 | + |
| 14 | +As with Perl, in Ruby,... |
| 15 | + |
| 16 | +* You’ve got a package management system, somewhat like CPAN |
| 17 | + (though it’s called [RubyGems][1]) |
| 18 | +* Regexes are built right in. Bon appétit! |
| 19 | +* There’s a fairly large number of commonly-used built-ins. |
| 20 | +* Parentheses are often optional |
| 21 | +* Strings work basically the same. |
| 22 | +* There’s a general delimited string and regex quoting syntax similar to |
| 23 | + Perl’s (looks like `%q{this (single-quoted)}`, or `%Q{this |
| 24 | + (double-quotish)}`, and `%w{this for a single-quoted list of words}`. |
| 25 | + You `%Q|can|` `%Q(use)` `%Q^other^` delimiters if you like). |
| 26 | +* You’ve got double-quotish variable interpolation, though it `"looks |
| 27 | + #{like} this"` (and you can put any Ruby code you like inside that |
| 28 | + `#{}`). |
| 29 | +* Shell command expansion uses \`backticks\`. |
| 30 | +* You’ve got embedded doc tools (Ruby’s is called rdoc). |
| 31 | + |
| 32 | +### Differences |
| 33 | + |
| 34 | +Unlike Perl, in Ruby,... |
| 35 | + |
| 36 | +* You don’t have the context-dependent rules like with Perl. |
| 37 | +* A variable isn’t the same as the object to which it refers. Instead, |
| 38 | + it’s always just a reference to an object. |
| 39 | +* Although `$` and <tt>@</tt> are used as the first character in |
| 40 | + variable names sometimes, rather than indicating type, they indicate |
| 41 | + scope (`$` for globals, <tt>@</tt> for object instance, and |
| 42 | + <tt>@@</tt> for class attributes). |
| 43 | +* Array literals go in brackets instead of parentheses. |
| 44 | +* Composing lists of other lists does not flatten them into one big |
| 45 | + list. Instead you get an array of arrays. |
| 46 | +* It’s `def` instead of `sub`. |
| 47 | +* There’s no semicolons needed at the end of each line. Incidentally, |
| 48 | + you end things like function definitions, class definitions, and case |
| 49 | + statements with the `end` keyword. |
| 50 | +* Objects are strongly typed. You’ll be manually calling `foo.to_i`, |
| 51 | + `foo.to_s`, etc., if you need to convert between types. |
| 52 | +* There’s no `eq`, `ne`, `lt`, `gt`, `ge`, nor `le`. |
| 53 | +* There’s no diamond operator. You usually use <tt>IO.*some\_func*</tt> |
| 54 | + instead. |
| 55 | +* The fat comma is only used for hash literals. |
| 56 | +* There’s no `undef`. In Ruby you have `nil`. `nil` is an object (like |
| 57 | + anything else in Ruby). It’s not the same as an undefined variable. It |
| 58 | + evaluates to `false` if you treat it like a boolean. |
| 59 | +* When tested for truth, only `false` and `nil` evaluate to a false |
| 60 | + value. Everything else is true (including `0`, `0.0`, and `"0"`). |
| 61 | +* There’s no [PerlMonks][2]. Though the ruby-talk mailing list is a very |
| 62 | + helpful place. |
| 63 | + |
| 64 | +[1]: http://docs.rubygems.org/ |
| 65 | +[2]: http://www.perlmonks.org/ |
0 commit comments