package Location;
use 5.008001;
use strict;
use vars '$AUTOLOAD';
use warnings;
use Carp;
use UNIVERSAL qw/ isa /;
use integer; #we use integer coordinates
use constant nl=>"\n";
use constant X=>0; # these are the array fields
use constant Y=>1;
####
# defaults
{
my $default_location=[
0,
0
];
sub get_default_location{
return [$default_location->[X],$default_location->[Y]];
}
sub get_default_X{
return $default_location->[X];
}
sub get_default_Y{
return $default_location->[Y];
}
}
##
##
sub new{
use integer;
my ($self,$x,$y)=@_;
# if $x is an arrayref use this
if (isa($x,'ARRAY')){
return bless $x,$self;
}
# otherwise try the parameters or use defaults
return bless[
$x||get_default_X,
$y||get_default_Y
],$self;
}
##
##
sub get_location{
my $self=shift;
return [$self->[X],$self->[Y]];
}
sub get_X{
my $self=shift;
return $self->get_location->[X];
}
sub get_Y{
my $self=shift;
return $self->get_location->[Y];
}
##
##
sub set_location{
my ($self,$x,$y)=@_;
if(isa($x,"PLocation")){
$self->[X]=$x->get_X;
$self->[Y]=$x->get_Y;
}
elsif(defined $x and defined $y){
$self->[X]=$x;
$self->[Y]=$y;
}
else {
croak "Insufficient Arguments: set_location(PLocation->new($x,$y)) or set_location($x,$y)".nl;
}
}
sub set_X{
my ($self,$x)=@_;
$self->[X]=$x;
}
sub set_Y{
my ($self,$y)=@_;
$self->[Y]=$y;
}
# so far so nice
1;
##
##
use integer; #we use integer coordinates
use constant nl=>"\n";
use constant X=>0; # these are the array fields
use constant Y=>1;
# overload => thats new !
use overloead '<=>' => 'compare';
##
##
sub compare {
my ($loc1,$loc2,$rev)=@_;
croak "Cant do this with autoloaded comparison\n" if $rev;
if($loc1->get_X < $loc2->get_X and $loc1->get_Y< $loc2->get_Y){
return 1;
}
elsif($loc1->get_X > $loc2->get_X and $loc1->get_Y> $loc2->get_Y
){
return -1;
}
elsif($loc1->get_X = $loc2->get_X and $loc1->get_Y= $loc2->get_Y
){
return 0;
return ;
}