1Package::New(3) User Contributed Perl Documentation Package::New(3)
2
3
4
6 Package::New - Simple base package from which to inherit
7
9 package My::Package;
10 use base qw{Package::New}; #provides new and initialize
11
13 The Package::New object provides a consistent constructor for objects.
14
15 I find that I always need these two methods in every package that I
16 build. I plan to use this package as the base for all of my CPAN
17 packages.
18
20 Sane defaults
21 I recommend that you have sane default for all of your object
22 properties. I recommend using code like this.
23
24 sub myproperty {
25 my $self=shift;
26 $self->{"myproperty"}=shift if @_;
27 $self->{"myproperty"}="Default Value" unless defined $self->{"myproperty"};
28 return $self->{"myproperty"};
29 }
30
31 use strict and warnings
32 I recommend to always use strict, warnings and our version.
33
34 package My::Package;
35 use base qw{Package::New};
36 use strict;
37 use warnings;
38 our $VERSION='0.01';
39
40 Lazy Load where you can
41 I recommend Lazy Loading where you can.
42
43 sub mymethod {
44 my $self=shift;
45 $self->load unless $self->loaded;
46 return $self->{"mymethod"};
47 }
48
51 new
52 my $obj = Package::New->new(key=>$value, ...);
53
54 initialize
55 You can override this method in your package if you need to do
56 something after construction. But, lazy loading may be a better
57 option.
58
60 Log on RT and contact the author.
61
63 DavisNetworks.com provides support services for all Perl applications
64 including this package.
65
67 Michael R. Davis
68 CPAN ID: MRDVT
69 DavisNetworks.com
70 http://www.DavisNetworks.com/
71
73 This program is free software licensed under the...
74
75 The BSD License
76
77 The full text of the license can be found in the LICENSE file included
78 with this module.
79
81 Building Blocks
82 base, parent
83
84 Other Light Weight Base Objects Similar to Package::New
85 Package::Base, Class::Base, Class::Easy, Object::Tiny, Badger::Base
86
87 Heavy Base Objects - Drink the Kool-Aid
88 VSO, Class::Accessor::Fast, Class::Accessor, Moose, (as well as Moose-
89 alikes Moo, Mouse), Class::MethodMaker, Class::Meta
90
91 Even more
92 Spiffy, mixin, SUPER, Class::Trait, Class::C3, Moose::Role
93
94
95
96perl v5.34.0 2021-07-22 Package::New(3)