Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 2.22 KB  |  views: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env perl
  2.  
  3. use 5.010;
  4. use utf8;
  5. use strict;
  6. use warnings;
  7. use autodie;
  8.  
  9. my $j = do {
  10.     package MyJObject;
  11.  
  12.     use overload '""' => sub { shift->to_string };
  13.  
  14.     my %num_to_string = (
  15.         1 => 'one',
  16.         2 => 'two',
  17.         3 => 'three',
  18.         4 => 'four',
  19.         5 => 'five',
  20.         6 => 'six',
  21.         7 => 'seven',
  22.         8 => 'eight',
  23.         9 => 'nine',
  24.         0 => 'zero',
  25.     );
  26.  
  27.     sub new {
  28.         my ( $self, $val ) = @_;
  29.  
  30.         my $obj = bless { _val => 0 }, ref $self ? ref $self : $self;
  31.         $obj->val($val);
  32.  
  33.         return $obj;
  34.     }
  35.  
  36.     sub clone {
  37.         my $self = shift;
  38.  
  39.         return $self->new( $self->val );
  40.     }
  41.  
  42.     sub val {
  43.         my ( $self, $val ) = @_;
  44.  
  45.         if (defined $val) {
  46.             if ($val =~ /^\d+$/) {
  47.                 $self->{_val} = $val;
  48.             }
  49.             else {
  50.                 $self->{_val} = 0;
  51.             }
  52.         }
  53.  
  54.         return $self->{_val};
  55.     }
  56.  
  57.     sub to_string {
  58.         my $self = shift;
  59.  
  60.         return join '-', map { $num_to_string{$_} } split //, $self->val;
  61.     }
  62.  
  63.     sub equal_to {
  64.         my ( $self, $obj ) = @_;
  65.  
  66.         return !$self->_compare($obj);
  67.     }
  68.  
  69.     sub not_equal_to {
  70.         my ( $self, $obj ) = @_;
  71.  
  72.         return !$self->equal_to($obj);
  73.     }
  74.  
  75.     sub less_than {
  76.         my ( $self, $obj ) = @_;
  77.  
  78.         return $self->_compare($obj) == -1;
  79.     }
  80.  
  81.     sub greater_than {
  82.         my ( $self, $obj ) = @_;
  83.  
  84.         return $self->_compare($obj) == 1;
  85.     }
  86.  
  87.     sub less_than_or_equal_to {
  88.         my ( $self, $obj ) = @_;
  89.  
  90.         return $self->less_than($obj) || $self->equal_to($obj);
  91.     }
  92.  
  93.     sub greater_than_or_equal_to {
  94.         my ( $self, $obj ) = @_;
  95.  
  96.         return $self->greater_than($obj) || $self->equal_to($obj);
  97.     }
  98.  
  99.     sub _compare {
  100.         my ( $self, $obj ) = @_;
  101.  
  102.         return $self->val <=> $obj->val;
  103.     }
  104.  
  105.     sub debug {
  106.         my $self = shift;
  107.  
  108.         return $self->val . ": $self";
  109.     }
  110.  
  111.     __PACKAGE__->new;
  112. };
  113.  
  114.  
  115. say $j->debug;
  116. $j->val(13579);
  117. say $j->debug;
  118.  
  119. my $i = $j->new(24680);
  120. say $i->debug;
  121.  
  122. my $k = $j->clone;
  123. say $k->debug;
  124.  
  125. say "$j and $k are same"     if $j->equal_to($k);
  126. say "$j and $i are not same" if $j->not_equal_to($i);
  127. say "$i is greater than $j"  if $i->greater_than($j);