From: Tim Hammerquist Date: 2002-08-28T05:02:22+09:00 Subject: Re: Converting Perl scripts to Ruby? Peter Hickman graced us by uttering: > Tim Hammerquist wrote: > >>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; >> } >> > > For some programmers (myself included) the creation of a > surrogate variable, the implicit $_ or the explicit $x, is a > bad thing. It uses up valuable name real estate and in all > honesty only exists because the language mandates you should > have one. I'm not sure why you value name real estate so highly. Var names aren't like file descriptors. And with a language as robust as Ruby, space used for variable names can't possibly be the largest drain on resources. Namespace real estate can't be at that much of a premium either, because in both the Ruby and Perl examples, the variable goes out of scope as soon as the iteration finishes. Whatever happens to |account| after: accounts.each { |account| do_stuff } finishes also happens to $account at the end of this Perl loop: foreach my $account (@accounts) { do_stuff } > Idiomatic Perl is not the same as idiomatic Ruby. In the Ruby > the |account| local variable is not a surrogate to dereference > the account in question, it is the account. How do you mean "surrogate"? And no, the idiomatic styles of languages is not identical. Where ruby uses #each, perl uses a foreach loop. But these _are_ the idiomatic equivalents for the same procedure. > # ruby > accounts.each {| account | account.interest(interestrate)} > > # perl > $_->interest($interest_rate) for @accounts; In this, $_ becomes the "alias" for each individual element of @accounts. the '->' notation is a method of accessing elements in a perl object or reference; this makes each element of @accounts an object reference, which is aliased to $_ at each iteration. However, suppose you had this: my @arr = ("abc", "def", "ghi"); tr/a-z/A-Z/ for @arr; print "@arr"; --> ABC DEF GHI Whether you use the explicit $some_name or the implicit $_, any changes made to the var are made directly to the element. This is both very elegant, and very dangerous. If you want a harmless copy of an element to play with, you must make it yourself. > I agree your Perl code is cleaner but I was just hacking > something up to show a point. Unfortunately there's more Perl > code like mine in the world than there is like yours, mores the > pity. Yes, there are quite a few "perl hackers" entirely unfamiliar to idiomatic perl. This is what gives perl a bad name: perl programs written in C. >>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. > > Perl OO reminds me of C++ written by seasoned C programmers. > (mutters something about old dogs and new tricks) Yes, C really doesn't translate well into many other languages. See above. ;-) [f-up2 poster] Tim Hammerquist -- Windows 98 is *NOT* a virus - viruses are small and efficient.