--- nfo/perl/libs/DesignPattern/Object.pm 2002/12/13 21:46:29 1.1 +++ nfo/perl/libs/DesignPattern/Object.pm 2002/12/27 16:05:42 1.2 @@ -1,7 +1,10 @@ ## -------------------------------------------------------------------------------- -## $Id: Object.pm,v 1.1 2002/12/13 21:46:29 joko Exp $ +## $Id: Object.pm,v 1.2 2002/12/27 16:05:42 joko Exp $ ## -------------------------------------------------------------------------------- ## $Log: Object.pm,v $ +## Revision 1.2 2002/12/27 16:05:42 joko +## + played with Devel::CallerItem and Devel::StackTrace +## ## Revision 1.1 2002/12/13 21:46:29 joko ## + initial check-in ## @@ -13,6 +16,73 @@ use strict; use warnings; + +use Data::Dumper; +#use Devel::CallerItem; +#use Devel::StackTrace; + +sub new { + + # the classname in most cases + my $classname = shift; + + # use already blessed reference, if passed in - else use the very classname + my $class = ref ($classname) || $classname; + + # the base for our object - a plain perl hash, which .... + my $self = {}; + # note: + # this makes an instance of an arbitrary perl variable, + # the most often used for this purpose is - guess it - the hash, + # since it resembles object-properties in a convenient way: + # $object->{property} = 'Hello World!'; + # if you _do_ care about privacy you might take a look + # at CPAN's Tie::SecureHash or Class::Contract ... have fun! + + # TODO: what about logging in here? inherit from + # DesignPattern::Object::Logger for this purpose.... + # ... or would this give us (harmful) circular module dependencies??? + #$logger->debug( __PACKAGE__ . "->new( @args )" ); # this is not "common"! + + # remember the caller + $self->{'__caller'} = caller; + #print Dumper(caller(2)); + #exit; + + # remember the stacktrace: abstract this out (DesignPattern::Object::Trace) or parametrize! + #my $trace = Devel::StackTrace->new(); + #print Dumper($trace); + #print Dumper($trace->frame(1)->args()); + +#print "args: ", $self->{'__caller'}->hasargs(), "\n"; + + #print Dumper($self); + #exit; + + # argument-handling ... + + # ... get them ... + my @args = (); + @_ && (@args = @_); + + # ... check if we can coerce them into an array (this needs an even number of arguments) + my $argcount = $#args + 1; + my $fract = $argcount / 2; + + my $seperate = pop @args if $fract != int($fract); + + # mixin arguments + $self = { @args }; + + # mixin seperate + $self->{'__arg'} = $seperate if $seperate; + + # ... bless hash into object using classname + bless $self, $class; + $self->_init() if $self->can('_init'); + return $self; +} + sub _abstract_function { my $self = shift; my $self_classname = ref $self;