/[cvs]/nfo/perl/libs/DesignPattern/Object.pm
ViewVC logotype

Diff of /nfo/perl/libs/DesignPattern/Object.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by joko, Fri Dec 13 21:46:29 2002 UTC revision 1.7 by joko, Tue Feb 18 18:34:35 2003 UTC
# Line 1  Line 1 
1  ## --------------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
2  ##  $Id$  ##  $Id$
3  ## --------------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
4  ##  $Log$  ##  $Log$
5    ##  Revision 1.7  2003/02/18 18:34:35  joko
6    ##  + fix: just logs if possible (sub log_basic)
7    ##
8    ##  Revision 1.6  2003/02/11 11:04:27  joko
9    ##  + metadata (args, caller, etc.) are now stored inside {__bridge}
10    ##
11    ##  Revision 1.5  2003/02/09 16:24:46  joko
12    ##  + pseudo constructor mechanism by calling method 'constructor' on object instantiation
13    ##
14    ##  Revision 1.4  2003/01/22 17:56:49  root
15    ##  + fix: just use the logger if it's available
16    ##
17    ##  Revision 1.3  2003/01/20 16:54:22  joko
18    ##  + sub fromPackage: refactored from libp's 'getNewPerlObjFromPkgName' or s.th.l.th.
19    ##
20    ##  Revision 1.2  2002/12/27 16:05:42  joko
21    ##  + played with Devel::CallerItem and Devel::StackTrace
22    ##
23  ##  Revision 1.1  2002/12/13 21:46:29  joko  ##  Revision 1.1  2002/12/13 21:46:29  joko
24  ##  + initial check-in  ##  + initial check-in
25  ##  ##
26  ## --------------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
27    
28    
29  package DesignPattern::Object;  package DesignPattern::Object;
# Line 13  package DesignPattern::Object; Line 31  package DesignPattern::Object;
31  use strict;  use strict;
32  use warnings;  use warnings;
33    
34    
35    use Data::Dumper;
36    #use Devel::CallerItem;
37    #use Devel::StackTrace;
38    
39    
40    my $_dp_globals;
41    
42    $_dp_globals = {
43      TRACE => 0,
44    };
45    
46    
47    sub new {
48      
49      # the classname in most cases
50      my $classname = shift;
51      
52      # use already blessed reference, if passed in - else use the very classname
53      my $class = ref ($classname) || $classname;
54      
55      #$self->log_basic( "$classname->new( ... )" . "\t[via " . __PACKAGE__ . "]" , 'debug');
56      
57      # the base for our object - a plain perl hash, which ....
58      my $self = {};
59      # note:
60      # this makes an instance of an arbitrary perl variable,
61      # the most often used for this purpose is - guess it - the hash,
62      # since it resembles object-properties in a convenient way:
63      #   $object->{property} = 'Hello World!';
64      # if you _do_ care about privacy you might take a look
65      # at CPAN's Tie::SecureHash or Class::Contract ...   have fun!
66      
67      # TODO: what about logging in here? inherit from
68      # DesignPattern::Object::Logger for this purpose....
69      # ... or would this give us (harmful) circular module dependencies???
70      #$self->log_basic( __PACKAGE__ . "->new( @args )", 'debug' );      # this is not "common"!
71    
72        
73      # remember the stacktrace: abstract this out (DesignPattern::Object::Trace) or parametrize!
74      #my $trace = Devel::StackTrace->new();
75      #print Dumper($trace);
76      #print Dumper($trace->frame(1)->args());
77    
78    #print "args: ", $self->{'__caller'}->hasargs(), "\n";
79    
80      #print Dumper($self);
81      #exit;
82    
83    
84      # argument-handling ...
85    
86        # ... get them ...
87        my @args = ();
88        @_ && (@args = @_);
89        
90        # ... check if we can coerce them into an array (this needs an even number of arguments)
91      my $argcount = $#args + 1;
92      my $fract = $argcount / 2;
93      
94      my $seperate = pop @args if $fract != int($fract);
95    
96      # mixin arguments
97      $self = { @args };
98      
99      # scan for arguments prefixed by a double underscore '__'
100      foreach (keys %$self) {
101        if (/^__(.+?)$/) {
102          $self->{__bridge}->{$1} = $self->{$_};
103          delete $self->{$_};
104        }
105      }
106      
107      # mixin seperate - FIXME: should this not be done after blessing?
108      $self->{__bridge}->{'arg'} = $seperate if $seperate;
109    
110      # ... bless hash into object using classname
111      bless $self, $class;
112    
113      # remember the caller
114        $self->{__bridge}->{caller_module}  = caller;
115        #print Dumper(caller(2));
116        #exit;
117    
118      $self->{__bridge}->{class_name} = $classname;
119    
120      # patches for backward compatibility
121        $self->{'__arg'} = $self->{__bridge}->{'arg'} if $self->{__bridge}->{'arg'};
122        $self->{'__caller'} = $self->{__bridge}->{caller_module} if $self->{__bridge}->{caller_module};
123        $self->{'__classname'} = $self->{__bridge}->{class_name} if $self->{__bridge}->{class_name};
124    
125      $self->_init() if $self->can('_init');
126      $self->constructor() if $self->can('constructor');
127    
128      # trace
129        #print Dumper($self);
130        #exit;
131    
132      return $self;
133    }  
134    
135  sub _abstract_function {  sub _abstract_function {
136    my $self = shift;    my $self = shift;
137    my $self_classname = ref $self;    my $self_classname = ref $self;
138    my $function_name = shift;    my $function_name = shift;
139    # was:    # was:
140    # $logger->error( __PACKAGE__ . ": function \"$fName\" is an abstract method, please implement it in \"$class\"");    $self->log_basic( __PACKAGE__ . ": function '$function_name' is an abstract method, please implement it in '$self_classname'.", 'warning');
141    # is:    # is:
142    die( __PACKAGE__ . ": Function '$function_name' is an abstract method, please implement it in '$self_classname'.");    #die( __PACKAGE__ . ": Function '$function_name' is an abstract method, please implement it in '$self_classname'.");
143    #exit;    #exit;
144  }  }
145    
146    sub fromPackage {
147      my $self = shift;
148      #my $self_classname = ref $self;
149      my $pkgname = shift;
150      my @args = @_;
151    #  my $args = shift;
152    
153    #      my $caller = $self->{'__caller'};
154    
155    #print Dumper($args);
156    
157      $self->log_basic( __PACKAGE__ . "->fromPackage( pkgname $pkgname args @args )", 'debug');
158      
159      # perl-load
160        my $evstring = "use $pkgname;";
161        eval($evstring);
162      
163      # report errors
164        if ($@) {
165          # build error-messages
166          my $errmsg_native = __PACKAGE__ . ':' . __LINE__ . " Error in eval \"$evstring\": " .  $@;
167          #my $classname = $self->{__classname};
168          my $errmsg_critical = '';
169          if ($errmsg_native =~ m/Can't locate (.+?) in \@INC/) {
170            $errmsg_critical = "Could not instantiate object from package '$pkgname' ('$1' not found).";
171          } else {
172            $errmsg_critical = $errmsg_native;
173          }
174          # write error to logging-output (console|file|database)
175          $self->log_basic( $errmsg_native, 'debug' );
176          $self->log_basic( $errmsg_critical, 'warning' );
177          return;
178        }
179      
180      # object-creation
181        my $object = $pkgname->new(@args);
182    
183      # run boot-methods on object
184        $object->_init() if $object->can('_init');
185        $object->constructor() if $object->can('constructor');
186    
187      return $object;
188    }
189    
190    sub log_basic {
191      my $self = shift;
192      my $message = shift;
193      my $level = shift;
194    
195      if ($_dp_globals->{TRACE} || ($level && $level =~ /warning|error|critical/)) {
196        print $level, ": ", $message, "\n";
197      }
198    
199      # get logger instance
200      if (!$_dp_globals->{logger}) {
201        $_dp_globals->{logger} = eval { Log::Dispatch::Config->instance; };
202      }
203      
204      if ($_dp_globals->{logger}) {
205        $_dp_globals->{logger}->log($level, $message);
206      #} else {
207        #print $level, ": ", $message, "\n";
208      }
209      
210    }
211    
212  1;  1;
213    __END__

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.7

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed