2 |
## $Id$ |
## $Id$ |
3 |
## -------------------------------------------------------------------------------- |
## -------------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.2 2002/12/27 16:05:42 joko |
6 |
|
## + played with Devel::CallerItem and Devel::StackTrace |
7 |
|
## |
8 |
## Revision 1.1 2002/12/13 21:46:29 joko |
## Revision 1.1 2002/12/13 21:46:29 joko |
9 |
## + initial check-in |
## + initial check-in |
10 |
## |
## |
16 |
use strict; |
use strict; |
17 |
use warnings; |
use warnings; |
18 |
|
|
19 |
|
|
20 |
|
use Data::Dumper; |
21 |
|
#use Devel::CallerItem; |
22 |
|
#use Devel::StackTrace; |
23 |
|
|
24 |
|
sub new { |
25 |
|
|
26 |
|
# the classname in most cases |
27 |
|
my $classname = shift; |
28 |
|
|
29 |
|
# use already blessed reference, if passed in - else use the very classname |
30 |
|
my $class = ref ($classname) || $classname; |
31 |
|
|
32 |
|
# the base for our object - a plain perl hash, which .... |
33 |
|
my $self = {}; |
34 |
|
# note: |
35 |
|
# this makes an instance of an arbitrary perl variable, |
36 |
|
# the most often used for this purpose is - guess it - the hash, |
37 |
|
# since it resembles object-properties in a convenient way: |
38 |
|
# $object->{property} = 'Hello World!'; |
39 |
|
# if you _do_ care about privacy you might take a look |
40 |
|
# at CPAN's Tie::SecureHash or Class::Contract ... have fun! |
41 |
|
|
42 |
|
# TODO: what about logging in here? inherit from |
43 |
|
# DesignPattern::Object::Logger for this purpose.... |
44 |
|
# ... or would this give us (harmful) circular module dependencies??? |
45 |
|
#$logger->debug( __PACKAGE__ . "->new( @args )" ); # this is not "common"! |
46 |
|
|
47 |
|
# remember the caller |
48 |
|
$self->{'__caller'} = caller; |
49 |
|
#print Dumper(caller(2)); |
50 |
|
#exit; |
51 |
|
|
52 |
|
# remember the stacktrace: abstract this out (DesignPattern::Object::Trace) or parametrize! |
53 |
|
#my $trace = Devel::StackTrace->new(); |
54 |
|
#print Dumper($trace); |
55 |
|
#print Dumper($trace->frame(1)->args()); |
56 |
|
|
57 |
|
#print "args: ", $self->{'__caller'}->hasargs(), "\n"; |
58 |
|
|
59 |
|
#print Dumper($self); |
60 |
|
#exit; |
61 |
|
|
62 |
|
# argument-handling ... |
63 |
|
|
64 |
|
# ... get them ... |
65 |
|
my @args = (); |
66 |
|
@_ && (@args = @_); |
67 |
|
|
68 |
|
# ... check if we can coerce them into an array (this needs an even number of arguments) |
69 |
|
my $argcount = $#args + 1; |
70 |
|
my $fract = $argcount / 2; |
71 |
|
|
72 |
|
my $seperate = pop @args if $fract != int($fract); |
73 |
|
|
74 |
|
# mixin arguments |
75 |
|
$self = { @args }; |
76 |
|
|
77 |
|
# mixin seperate |
78 |
|
$self->{'__arg'} = $seperate if $seperate; |
79 |
|
|
80 |
|
# ... bless hash into object using classname |
81 |
|
bless $self, $class; |
82 |
|
$self->_init() if $self->can('_init'); |
83 |
|
return $self; |
84 |
|
} |
85 |
|
|
86 |
sub _abstract_function { |
sub _abstract_function { |
87 |
my $self = shift; |
my $self = shift; |
88 |
my $self_classname = ref $self; |
my $self_classname = ref $self; |