develooper Front page | perl.fwp | Postings from April 2003

[spoiler] Ruby.pm

Thread Next
From:
Simon Cozens
Date:
April 10, 2003 11:05
Subject:
[spoiler] Ruby.pm
Message ID:
20030410180540.GD5842@deep-dark-truthful-mirror.pad
package Ruby;
sub import { overload::constant(
    integer => sub { return Fixnum->new(shift) },
          q => sub { return String->new(shift) });
}

package Object;
sub class { return Class->TIESCALAR(ref $_[0]) } 

package Fixnum;
use Tie::Scalar;
@ISA = qw(Tie::StdScalar Object);
sub new { goto &{$_[0]->can("TIESCALAR")} } # Hack
use overload '""' => sub { ${$_[0]} }, 
             "0+" => sub { ${$_[0]} },
             "." => sub { my ($obj,$meth)=@_; $obj->$meth},
             "++" => sub { $_[0]->STORE(1+$_[0]->FETCH) }, 
                # Bad, should use "=" overloading instead.
            fallback => 1;
sub FETCH { return $_[0] }
sub STORE { $_[0] =bless \do{my $x = $_[1]}, ref $_[0];}

sub puts { print $_[0],"\n" }

sub upto {
    my ($from, $to, $block) = @_;
    $block->($_) for (map { Fixnum->TIESCALAR($_) } $from..$to);
}


package String;
@ISA = qw(Fixnum); # Hack, but it'll do

sub length  { return new Fixnum (length  $_[0]) }
sub reverse { return new String (reverse $_[0]) }

package UNIVERSAL;
use  Attribute::Handlers autotie => { "Fixnum" => "Fixnum",
                                      "String" => "String" };

package Class;
@ISA = qw(String); # Also a hack

sub to_s { return new String ("".$_[0]) } 

1;

-- 
For true believers, LORD would be K\textsc{nuth} in TeX, and
L\textsc{amport} in LaTeX. Atheists prefer \phantom{LORD}. Agnostics
may need to use the ifthen package.
 - Chris Boyd, comp.text.tex

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About