From: Tim Hammerquist Date: 2002-08-24T10:59:54+09:00 Subject: Re: Converting Perl scripts to Ruby? [ the points below are included to clarify to Ruby users who may not know Perl. ] Peter Hickman graced us by uttering: [ snip ] > This in Perl > > for(my $x = 0; $x < scalar(@accounts}; $x++) { > $accounts[$x]->interest = > ($accounts[$x]->holding * $interestrate); > } > > becomes > > accounts.each {| account | account.interest(interestrate)} Why haven't you created a Perl object for this and defined an #interest method for it as well? > Ok there are other ways of writting that loop in Perl but they > all introduce an unnecessary variable such as $x even if it is > the implicit $_. I'm not sure what you're gripe is here. Ruby's iterators also use an extra variable. That's what appears between the ||'s. But for the sake of accurate equivalents, let's use _idiomatic_ Perl. You've neglected to show this much cleaner version: foreach my $x (@accounts) { $x->interest = $x->holding * $interest_rate; } And using implicit use of $_: foreach (@accounts) { $_->interest = $_->holding * $interest_rate; } or an OO version: $_->interest($interest_rate) for @accounts; # see [*] At the end of all this, Ruby still looks (and works) cleaner than any Perl example thus far. [*] 'for' and 'foreach' are aliases for the same element. Thus, you can write: foreach (my $i; $i < $max; $i++) { ... } as well as: for my $x (@arr_of_stuff) { ... } [ snip ] > Even good OO Perl tends to contain much 'paperwork' that Ruby > handles for you. You will never have to bless something again. Very true. I can't say this enough: Perl's OO model is a gigantic ball of bovine excrement, even if it does do some nifty tricks. If the rest of Perl weren't so useful, I'd have nothing to do with it. Correction: Perl5's OO model sucks popsicles. I'm anxious to see the improvements on it in Perl6 (as any change to Perl5's OO _must_ be an improvement). =) HTH Tim Hammerquist -- If someone stinks, view it as a reason to help them, not a reason to avoid them. -- Larry Wall in <199702111730.JAA28598@wall.org>