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