Author: dylan
Date: 2005-02-05 17:13:13 -0500 (Sat, 05 Feb 2005)
New Revision: 618

Modified:
   trunk/main/core/lib/Haver/Base.pm
Log:
new Haver::Base code.


Modified: trunk/main/core/lib/Haver/Base.pm
===================================================================
--- trunk/main/core/lib/Haver/Base.pm   2005-02-02 19:38:55 UTC (rev 617)
+++ trunk/main/core/lib/Haver/Base.pm   2005-02-05 22:13:13 UTC (rev 618)
@@ -3,41 +3,46 @@
 package Haver::Base;
 use strict;
 use warnings;
+use Carp;
 
-our $VERSION = "0.01";
+use constant DEBUG => 1;
 
-sub new {
-       my $this = shift;
-       #ASSERT: @_ == 1 || (@_ % 2) == 0;
-       my $me   = @_ == 1 && ref($_[0]) ?  shift : { @_ };
-       my $type = ref $me;
-       bless $me, ref($this) || $this;
+our $VERSION = 0.08;
 
-       delete $me->{$_} foreach (grep(/^_/, keys %$me));
 
-       if ($type eq 'HASH') {
-               if ([EMAIL PROTECTED] and exists $me->{'-initargs'}) {
-                       @_ = @{ delete $me->{'-initargs'} };
-               }
-       }
-       
-       #DEBUG(obj): "New: ", overload::StrVal($me);
+sub new {
+       my $class = shift;
+       my $me    = bless {}, $class;
+    my $args;
+    
+    if (UNIVERSAL::isa($_[0], 'HASH')) {
+        $args = $_[0];
+    } elsif ((@_ % 2) == 0) {
+        $args = { @_ };
+    } else {
+        croak "Odd number of parameters to new()";
+    }
+    
+    if (DEBUG) {
+        print "New: ", overload::StrVal($me), "\n";
+    }
 
-       $me->initialize(@_);
+       $me->initialize($args);
 
        return $me;
 }
 
 
 
-sub initialize {undef}
-sub finalize   {undef}
+sub initialize { return }
+sub finalize   { return }
 
 sub DESTROY {
        my $me = shift;
        
-       
-       #DEBUG(obj): "Destroy: ", overload::StrVal($me);
+    if (DEBUG) {
+        print "Destroy: ", overload::StrVal($me), "\n";
+    }
        $me->finalize();
 }
 
@@ -52,28 +57,21 @@
 
 =head1 SYNOPSIS
 
-   BEGIN { 
-   package Foobar;
-   use base 'Haver::Base';
+    BEGIN { 
+        package Foobar;
+        use base 'Haver::Base';
 
-   sub initialize {
-       my ($me, @args) = @_;
-       print "init args: join(', ', @args), "\n";
-   }
-
-   sub finalize {
-      my ($me) = @_;
-      # do stuff here that you would do in DESTROY.
-   }
+        sub initialize {
+            my ($me, $params) = @_;
+            print "init args: join(', ', keys %$params), "\n";
+        }
+        
+        sub finalize {
+            my ($me) = @_;
+            # do stuff here that you would do in DESTROY.
+        }
    } # BEGIN
 
-   my $foo = new Foobar({name => "Foobar"}, 1, 2, 3);
-   # or: my $foo = new Foobar(name => "Foobar", -initargs => [1, 2, 3])
-   # "init args: 1, 2, 3" was printed.
-   $foo->{name} eq "Foobar";
-
-   
-
 =head1 DESCRIPTION
 
 This is a base class for nearly every class in the Haver server,
@@ -89,7 +87,7 @@
 
 Haver::Base implements the following methods:
 
-=head2 $class->new(%options)
+=head2 new(%params || \%params)
 
 This constructor method creates and returns a new I<$class>,
 initialized with the list of key-value pairs in I<%options>.
@@ -97,41 +95,44 @@
 -initargs can be specified as an array reference,
 and will be passed to initialize.
 
-
 Alternatively, new() may be called
 with as C<$class-E<gt>new($option, @initargs)>
 where I<$option> is a hashref used like I<%options>
 and I<@initargs> is a list of arguments to be passed to
 initialize()
 
+=head3 Parameters
+
+As a matter of style, parameters should begin with a single dash (-)
+and be all lower-case.
+
+Further, you should document parameters in a "PARAMETERS"
+section in the POD of the class. Place it right before "METHODS".
+
 =head1 VIRTUAL METHODS
 
 The following methods may be defined by subclasses:
 
-=head2 $self->initialize(@args)
+=head2 initialize($params)
 
-This is called by new(), and should
-be used to do any needed initialization things.
-Optionally, I<@args> will contain arguments
-passed to new(), as explained above.
+This is called by new(), and should be defined instead of new().
 
-In practice, no current subclass of Haver::Base
-has an initalize method that takes arguments.
+The first argument, $params, is a hashref all any parameters passed to new().
 
-=head2 $self->finalize(Z<>)
+The return value is unimportant.
 
+=head2 finalize(Z<>)
+
 This method is called from DESTROY().
 
 You should overload this method instead of DESTROY(),
 so that the useful debugging messages are printed
 when objects are destroyed.
 
-   
 =head1 SEE ALSO
 
 L<https://gna.org/projects/haver/>
 
-
 =head1 AUTHOR
 
 Dylan William Hardison, E<lt>[EMAIL PROTECTED]<gt>


Reply via email to