Created
April 16, 2014 13:40
-
-
Save breezhang/10876964 to your computer and use it in GitHub Desktop.
basic perl app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package myint; | |
use strict; | |
use warnings; | |
sub import { | |
$^H{"myint/in_effect"} = 1; | |
} | |
sub unimport { | |
$^H{"myint/in_effect"} = 0; | |
} | |
sub in_effect { | |
my $level = shift // 0; | |
my $hinthash = (caller($level))[10]; | |
return $hinthash->{"myint/in_effect"}; | |
} | |
package MyMaths; | |
use warnings; | |
use strict; | |
use overload '+' => sub { | |
my ( $l, $r ) = @_; | |
# Pass 1 to check up one call level from here | |
if ( myint::in_effect(1) ) { | |
int($$l) + int($$r); | |
} | |
else { | |
$$l + $$r; | |
} | |
}; | |
sub new { | |
my ( $class, $value ) = @_; | |
bless \$value, $class; | |
} | |
package main; | |
use Data::Dumper::Concise; | |
my $o1 = MyMaths->new(1) ; | |
my $o2 = MyMaths->new(2) ; | |
print $o1 + $o2; | |
print "\n"; | |
print Dumper $o1; | |
print Dumper $o2; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment