Created
December 17, 2013 19:20
-
-
Save Danny02/8010964 to your computer and use it in GitHub Desktop.
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
use strict; | |
use warnings; | |
package VarVal; | |
sub println | |
{ | |
print @_, "\n"; | |
} | |
sub val | |
{ | |
my ($name) = @_; | |
{ | |
my %values; | |
*$name = sub | |
{ | |
my ($self) = @_; | |
$values{$self}; | |
} | |
} | |
} | |
my $cleaUpFuncs = []; | |
sub var | |
{ | |
my ($name) = @_; | |
{ | |
my %values; | |
*$name = sub :lvalue | |
{ | |
my ($self) = @_; | |
$values{$self}; | |
}; | |
push @$cleaUpFuncs, sub{ | |
println "deleting $name"; | |
my $self = shift; | |
delete $values {$self}; | |
} | |
} | |
} | |
sub DESTROY { | |
my $self = shift; | |
$_->($self) for @$cleaUpFuncs; | |
} | |
package Auto; | |
@ISA=(VarVal); | |
var tireCount; | |
var color; | |
sub new | |
{ | |
my ($class) = @_; | |
my $self = bless {}, $class; | |
$self->color = "blue"; | |
return $self; | |
} | |
package main; | |
sub println | |
{ | |
print @_, "\n"; | |
} | |
my $myCar = new Auto; | |
my $yourCar = new Auto; | |
$yourCar->color = "red"; | |
println "--"; | |
$yourCar->DESTROY(); | |
println "++"; | |
println $myCar->color(); | |
println $yourCar->color(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment