[VPM] Ruby in Perl

Peter Scott Peter at PSDT.com
Thu Dec 15 18:02:06 PST 2005


Passing along something that came across the VLUG list here (for the 
many of you that are on that list as well, you've seen this).

It was in response to this:


>At 01:53 PM 12/15/2005, Deryk Barker wrote:
>Noel Burton-Krahn wrote:
>
>>Thanks for your presentation and your follow-up, Steven!
>>
>>Yes, that method_missing does look evil (as does overriding base 
>>class functions outside its declaration), but it's interesting to see 
>>how it works.  Just because a language gives you a gun doesn't mean 
>>you have to shoot yourself!
>>
>>In fact, you can shoot yourself in a similar way with perl:
>
>But can you, for instance, open up the number class and redefine + on 
>integers in perl?


I replied:

<HHGTTG>
You're not going to like it.


You're really not going to like it.
</>

#!/usr/bin/perl -l
use strict;
use warnings;

{
   package Evil;

   use overload '+'      => sub { ${$_[0]} * ${$_[1]} },
                fallback => 1,
                '0+'     => sub { ${$_[0]} };

}

BEGIN {
   overload::constant( integer => sub { bless \$_[1], "Evil"; } );
}

my $x = 42 + 37;   # Looks like addition... really multiplication...
print $x;         # 1554



Speaking of evil, there was a great post on p5p a couple of days ago 
where a guy discovered that declaring labels and using goto on them 
inside the substitution clause of a s///e didn't work right.  Chip 
Salzenberg replied that this had just pegged his evil-o-meter.  Goes to 
show that there is still unexplored territory in Perl.

-- 
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/
http://www.perlmedic.com/



More information about the Victoria-pm mailing list