summaryrefslogtreecommitdiff
path: root/lib/Package/New.pm
blob: c98948dafc288a61ed7875b5b23956ee5a22907a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package Package::New;
use strict;
use warnings;

our $VERSION='0.08';

=head1 NAME

Package::New - Simple base package from which to inherit

=head1 SYNOPSIS

  package My::Package;
  use base qw{Package::New}; #provides new and initialize

=head1 DESCRIPTION

The Package::New object provides a consistent constructor for objects.

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. 

=head1 RECOMMENDATIONS

=head2 Sane defaults

I recommend that you have sane default for all of your object properties.  I recommend using code like this.

  sub myproperty {
    my $self=shift;
    $self->{"myproperty"}=shift if @_;
    $self->{"myproperty"}="Default Value" unless defined $self->{"myproperty"};
    return $self->{"myproperty"};
  }

=head2 use strict and warnings

I recommend to always use strict, warnings and our version.

  package My::Package;
  use base qw{Package::New};
  use strict;
  use warnings;
  our $VERSION='0.01';

=head2 Lazy Load where you can

I recommend Lazy Loading where you can.

  sub mymethod {
    my $self=shift;
    $self->load unless $self->loaded;
    return $self->{"mymethod"};
  }

=head1 USAGE

=head1 CONSTRUCTOR

=head2 new

  my $obj = Package::New->new(key=>$value, ...);

=cut

sub new {
  my $this=shift;
  my $class=ref($this) || $this;
  my $self={};
  bless $self, $class;
  $self->initialize(@_);
  return $self;
}

=head2 initialize

You can override this method in your package if you need to do something after construction.  But, lazy loading may be a better option.

=cut

sub initialize {
  my $self=shift;
  %$self=@_;
}

=head1 BUGS

Log on RT and contact the author.

=head1 SUPPORT

DavisNetworks.com provides support services for all Perl applications including this package.

=head1 AUTHOR

  Michael R. Davis
  CPAN ID: MRDVT
  DavisNetworks.com
  http://www.DavisNetworks.com/

=head1 COPYRIGHT

This program is free software licensed under the...

  The BSD License

The full text of the license can be found in the LICENSE file included with this module.

=head1 SEE ALSO

=head2 Building Blocks

L<base>, L<parent>

=head2 Other Light Weight Base Objects Similar to Package::New

L<Package::Base>, L<Class::Base>, L<Class::Easy>, L<Object::Tiny>, L<Badger::Base>

=head2 Heavy Base Objects - Drink the Kool-Aid

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>

=head2 Even more

L<Spiffy>, L<mixin>, L<SUPER>, L<Class::Trait>, L<Class::C3>, L<Moose::Role>

=cut

1;