package SimpleObject;
use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use Data::Dumper;
use Carp;
our @ISA = qw(Exporter);
sub create {
      my $class = shift;
      my @attributes = @_;
      my $self;   
      while (@attributes) {
	      my $r = shift (@attributes);
	      my $s = shift (@attributes);
	      $self->{$r} = $s;
      }
      
      bless $self, $class;
      return $self;
  }
sub create_copy {
	    my $self = shift;
	    croak "You cannot use create_copy as a class method! (called from $self)" unless (ref $self);
	    my $class = ref($self);
	    my %prehash = %{ dclone($self)}	;
	    create($class, %prehash);
}


sub attribute_exists {
    exists $_[0]->{$_[1]};
    }   		
sub get_attribute {
    $_[0]->{$_[1]}; 
}
sub set_attribute {
    $_[0]->{$_[1]} = $_[2];
}
sub add_to_attribute {
    $_[0]->{$_[1]} += $_[2];
}
sub drop_attribute {
    delete $_[0]->{$_[1]};
}
sub attribute_list {
    return keys %{$_[0]};
}     
sub id {
 $_[0]->{id}	
}
sub set_id {
	$_[0]->{id} = $_[1];
}  
sub dump_me_to {
    my $self = shift;
    my $outlet = shift;
    my $fh;
    my $closeme = 0;
    if (not defined $outlet) {
	    $fh = \*STDOUT;
    }
    elsif (ref $outlet eq "GLOB") {
	    $fh = $outlet;
    } 
    elsif (not ref $outlet) {
	    open ($fh, ">", $outlet) or die $!;
	    $closeme = 1;
    } 
    else {
	    die "Please provide a valid filename or filehandle to dump_me_to, or leave the argument blank for STDOUT!\n";
    }
    
    my $out = Data::Dumper->new([($self)]);
    print $fh $out->Dump;
    close $fh if ($closeme);
    1;
}
1;

