Syntax .. Smintax ..

Last week I was called upon to write some Perl - something I haven't done since last fall. It was funny how as I was working on it, it started to come back to me. So I thought it'd be fun to compare my favorite languages a little bit:

Looks at how arrays are defined and the way I like to loop through them:

PERL:
  1. @books = ('Learning Perl', 'Advanced Perl Programming', 'Perl Best Practices');
  2.  
  3. foreach $book(@books) {
  4.   print "* $book\n";
  5. }

Foreach is actually an alias for "For" and some prefer that in this use because it makes it more readable. It also looks like php...

Now, for the language that has consumed my time the past 6 months...

PHP:
  1. $books = array('Pro PHP Security','PHP Cookbook','Pro PHP XML and Web Services');
  2.  
  3. foreach($books as $book) {
  4.   print "* $book\n";
  5. }

And.... here's ruby:

RUBY:
  1. books = ['Programming Ruby','Ruby Cookbook','Mr. Neighborly\'s Humble Little Ruby Book']
  2.  
  3. for book in books
  4.   print "* #{book}\n"
  5. end

I never actually used the for loop like that with an array, I usually use this version which is what the parser converts it to anyways:

RUBY:
  1. books.each do |book|
  2.   print "* #{book}\n"
  3. end

Fun stuff :)

1 Comment »

  1. Yaakov Said,

    April 12, 2007 @ 6:50 am

    Perl:

    print “* $_\n” for @books;

RSS feed for comments on this post · TrackBack URI

Leave a Comment