/[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.3 by joko, Mon Jan 20 16:54:22 2003 UTC
# Line 1  Line 1 
1  ## --------------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
2  ##  $Id$  ##  $Id$
3  ## --------------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
4  ##  $Log$  ##  $Log$
5    ##  Revision 1.3  2003/01/20 16:54:22  joko
6    ##  + sub fromPackage: refactored from libp's 'getNewPerlObjFromPkgName' or s.th.l.th.
7    ##
8    ##  Revision 1.2  2002/12/27 16:05:42  joko
9    ##  + played with Devel::CallerItem and Devel::StackTrace
10    ##
11  ##  Revision 1.1  2002/12/13 21:46:29  joko  ##  Revision 1.1  2002/12/13 21:46:29  joko
12  ##  + initial check-in  ##  + initial check-in
13  ##  ##
14  ## --------------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
15    
16    
17  package DesignPattern::Object;  package DesignPattern::Object;
# Line 13  package DesignPattern::Object; Line 19  package DesignPattern::Object;
19  use strict;  use strict;
20  use warnings;  use warnings;
21    
22    
23    use Data::Dumper;
24    #use Devel::CallerItem;
25    #use Devel::StackTrace;
26    
27    
28    # get logger instance
29    my $logger = Log::Dispatch::Config->instance;
30    
31    sub new {
32      
33      # the classname in most cases
34      my $classname = shift;
35      
36      # use already blessed reference, if passed in - else use the very classname
37      my $class = ref ($classname) || $classname;
38      
39      $logger->debug( "$classname->new( ... )" . "\t[via " . __PACKAGE__ . "]" );
40      
41      # the base for our object - a plain perl hash, which ....
42      my $self = {};
43      # note:
44      # this makes an instance of an arbitrary perl variable,
45      # the most often used for this purpose is - guess it - the hash,
46      # since it resembles object-properties in a convenient way:
47      #   $object->{property} = 'Hello World!';
48      # if you _do_ care about privacy you might take a look
49      # at CPAN's Tie::SecureHash or Class::Contract ...   have fun!
50      
51      # TODO: what about logging in here? inherit from
52      # DesignPattern::Object::Logger for this purpose....
53      # ... or would this give us (harmful) circular module dependencies???
54      #$logger->debug( __PACKAGE__ . "->new( @args )" );      # this is not "common"!
55    
56        
57      # remember the stacktrace: abstract this out (DesignPattern::Object::Trace) or parametrize!
58      #my $trace = Devel::StackTrace->new();
59      #print Dumper($trace);
60      #print Dumper($trace->frame(1)->args());
61    
62    #print "args: ", $self->{'__caller'}->hasargs(), "\n";
63    
64      #print Dumper($self);
65      #exit;
66    
67    
68      # argument-handling ...
69    
70        # ... get them ...
71        my @args = ();
72        @_ && (@args = @_);
73        
74        # ... check if we can coerce them into an array (this needs an even number of arguments)
75      my $argcount = $#args + 1;
76      my $fract = $argcount / 2;
77      
78      my $seperate = pop @args if $fract != int($fract);
79    
80      # mixin arguments
81      $self = { @args };
82      
83      # mixin seperate
84      $self->{'__arg'} = $seperate if $seperate;
85    
86      # ... bless hash into object using classname
87      bless $self, $class;
88    
89      # remember the caller
90        $self->{'__caller'} = caller;
91        #print Dumper(caller(2));
92        #exit;
93    
94      $self->{__classname} = $classname;
95    
96      $self->_init() if $self->can('_init');
97    
98      return $self;
99    }  
100    
101  sub _abstract_function {  sub _abstract_function {
102    my $self = shift;    my $self = shift;
103    my $self_classname = ref $self;    my $self_classname = ref $self;
104    my $function_name = shift;    my $function_name = shift;
105    # was:    # was:
106    # $logger->error( __PACKAGE__ . ": function \"$fName\" is an abstract method, please implement it in \"$class\"");    $logger->warning( __PACKAGE__ . ": function '$function_name' is an abstract method, please implement it in '$self_classname'.");
107    # is:    # is:
108    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'.");
109    #exit;    #exit;
110  }  }
111    
112    sub fromPackage {
113      my $self = shift;
114      #my $self_classname = ref $self;
115      my $pkgname = shift;
116      my @args = @_;
117    #  my $args = shift;
118    
119    #      my $caller = $self->{'__caller'};
120    
121    #print Dumper($args);
122    
123      $logger->debug( __PACKAGE__ . "->fromPackage( pkgname $pkgname args @args )" );
124      
125      # perl-load
126        my $evstring = "use $pkgname;";
127        eval($evstring);
128      
129      # report errors
130        if ($@) {
131          # build error-messages
132          my $errmsg_native = __PACKAGE__ . ':' . __LINE__ . " Error in eval \"$evstring\": " .  $@;
133          #my $classname = $self->{__classname};
134          my $errmsg_critical = '';
135          if ($errmsg_native =~ m/Can't locate (.+?) in \@INC/) {
136            $errmsg_critical = "Could not instantiate object from package '$pkgname' - location of '$1' failed.";
137          } else {
138            $errmsg_critical = $errmsg_native;
139          }
140          # write error to logging-output (console|file|database)
141          $logger->debug( $errmsg_native );
142          $logger->critical( $errmsg_critical );
143          return;
144        }
145      
146      # object-creation
147        my $object = $pkgname->new(@args);
148    
149      # run boot-methods on object
150        $object->_init() if $object->can('_init');
151    
152      return $object;
153    }
154    
155  1;  1;

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

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