[libclass-tiny-perl] 05/19: pass constructor arguments to BUILD

gregor herrmann gregoa at debian.org
Sun May 31 14:03:11 UTC 2015


This is an automated email from the git hooks/post-receive script.

gregoa pushed a commit to annotated tag release-0.004
in repository libclass-tiny-perl.

commit 37540c751d8ca0a569ed6fb5abd60ac4538bca81
Author: David Golden <dagolden at cpan.org>
Date:   Tue Aug 20 17:07:46 2013 -0400

    pass constructor arguments to BUILD
---
 README.pod        | 10 ++++++----
 lib/Class/Tiny.pm | 14 ++++++++------
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/README.pod b/README.pod
index 79f082d..8b41171 100644
--- a/README.pod
+++ b/README.pod
@@ -183,13 +183,15 @@ the reference provided.
 
 =head2 BUILD
 
-If your class or any superclass defines a C<BUILD> method, they will be called
+If your class or any superclass defines a C<BUILD> method, it will be called
 by the constructor from the furthest parent class down to the child class after
-the object has been created.  No arguments are provided and the return value is
-ignored.  Use them for validation or setting default values.
+the object has been created.
+
+It is passed the constructor arguments as a hash reference.  The return value
+is ignored.  Use C<BUILD> for validation or setting default values.
 
     sub BUILD {
-        my $self = shift;
+        my ($self, $args) = @_;
         $self->foo(42) unless defined $self->foo;
         croak "Foo must be non-negative" if $self->foo < 0;
     }
diff --git a/lib/Class/Tiny.pm b/lib/Class/Tiny.pm
index 4089da5..8885ba3 100644
--- a/lib/Class/Tiny.pm
+++ b/lib/Class/Tiny.pm
@@ -76,11 +76,11 @@ sub new {
     }
 
     # create object and invoke BUILD
-    my $self = bless $args, $class;
+    my $self = bless { %$args }, $class;
     for my $s ( reverse @search ) {
         no strict 'refs';
         my $builder = *{ $s . "::BUILD" }{CODE};
-        $self->$builder if defined $builder;
+        $self->$builder($args) if defined $builder;
     }
 
     return $self;
@@ -262,13 +262,15 @@ the reference provided.
 
 =head2 BUILD
 
-If your class or any superclass defines a C<BUILD> method, they will be called
+If your class or any superclass defines a C<BUILD> method, it will be called
 by the constructor from the furthest parent class down to the child class after
-the object has been created.  No arguments are provided and the return value is
-ignored.  Use them for validation or setting default values.
+the object has been created.
+
+It is passed the constructor arguments as a hash reference.  The return value
+is ignored.  Use C<BUILD> for validation or setting default values.
 
     sub BUILD {
-        my $self = shift;
+        my ($self, $args) = @_;
         $self->foo(42) unless defined $self->foo;
         croak "Foo must be non-negative" if $self->foo < 0;
     }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libclass-tiny-perl.git



More information about the Pkg-perl-cvs-commits mailing list