| 1 | package Package::New; | ||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 3 | our $VERSION='0.08'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | Package::New - Simple base package from which to inherit | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | package My::Package; | ||||||||||||||||||||
| 14 | use base qw{Package::New}; #provides new and initialize | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | The Package::New object provides a consistent constructor for objects. | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | I find that I always need these two methods in every package that I build. I plan to use this package as the base for all of my CPAN packages. | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 RECOMMENDATIONS | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =head2 Sane defaults | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | I recommend that you have sane default for all of your object properties. I recommend using code like this. | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | sub myproperty { | ||||||||||||||||||||
| 29 | my $self=shift; | ||||||||||||||||||||
| 30 | $self->{"myproperty"}=shift if @_; | ||||||||||||||||||||
| 31 | $self->{"myproperty"}="Default Value" unless defined $self->{"myproperty"}; | ||||||||||||||||||||
| 32 | return $self->{"myproperty"}; | ||||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | =head2 use strict and warnings | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | I recommend to always use strict, warnings and our version. | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | package My::Package; | ||||||||||||||||||||
| 40 | use base qw{Package::New}; | ||||||||||||||||||||
| 41 | use strict; | ||||||||||||||||||||
| 42 | use warnings; | ||||||||||||||||||||
| 43 | our $VERSION='0.01'; | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | =head2 Lazy Load where you can | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | I recommend Lazy Loading where you can. | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | sub mymethod { | ||||||||||||||||||||
| 50 | my $self=shift; | ||||||||||||||||||||
| 51 | $self->load unless $self->loaded; | ||||||||||||||||||||
| 52 | return $self->{"mymethod"}; | ||||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =head1 USAGE | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =head1 CONSTRUCTOR | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =head2 new | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | my $obj = Package::New->new(key=>$value, ...); | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | sub new { | ||||||||||||||||||||
| 66 | 6 | ▶ | my $this=shift; | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 67 | 6 | ▶ | my $class=ref($this) || $this; | ||||||||||||||||||
|
Condition66%2/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table66%2/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 68 | 6 | my $self={}; | |||||||||||||||||||
| 69 | 6 | bless $self, $class; | |||||||||||||||||||
| 70 | 6 | $self->initialize(@_); | |||||||||||||||||||
| 71 | 6 | return $self; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =head2 initialize | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | You can override this method in your package if you need to do something after construction. But, lazy loading may be a better option. | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | sub initialize { | ||||||||||||||||||||
| 81 | 6 | ▶ | my $self=shift; | ||||||||||||||||||
|
Subroutine
initialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 82 | 6 | %$self=@_; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =head1 BUGS | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | Log on RT and contact the author. | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =head1 SUPPORT | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | DavisNetworks.com provides support services for all Perl applications including this package. | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =head1 AUTHOR | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | Michael R. Davis | ||||||||||||||||||||
| 96 | CPAN ID: MRDVT | ||||||||||||||||||||
| 97 | DavisNetworks.com | ||||||||||||||||||||
| 98 | http://www.DavisNetworks.com/ | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | =head1 COPYRIGHT | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | This program is free software licensed under the... | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | The BSD License | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | The full text of the license can be found in the LICENSE file included with this module. | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =head1 SEE ALSO | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =head2 Building Blocks | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | L<base>, L<parent> | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =head2 Other Light Weight Base Objects Similar to Package::New | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | L<Package::Base>, L<Class::Base>, L<Class::Easy>, L<Object::Tiny>, L<Badger::Base> | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =head2 Heavy Base Objects - Drink the Kool-Aid | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | L<VSO>, L<Class::Accessor::Fast>, L<Class::Accessor>, L<Moose>, (as well as Moose-alikes L<Moo>, L<Mouse>), L<Class::MethodMaker>, L<Class::Meta> | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =head2 Even more | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | L<Spiffy>, L<mixin>, L<SUPER>, L<Class::Trait>, L<Class::C3>, L<Moose::Role> | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 3 | 1; | |||||||||||||||||||