| 1 | # | ||
| 2 | # Mail::SPF::Test::Base | ||
| 3 | # Base class for Mail::SPF::Test classes. | ||
| 4 | # | ||
| 5 | # (C) 2006 Julian Mehnle <julian@mehnle.net> | ||
| 6 | # $Id: Base.pm 27 2006-12-23 20:11:21Z Julian Mehnle $ | ||
| 7 | # | ||
| 8 | ############################################################################## | ||
| 9 | |||
| 10 | package Mail::SPF::Test::Base; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Mail::SPF::Test::Base - Base class for Mail::SPF::Test classes | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | use warnings; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | use constant TRUE => (0 == 0); | ||
| 22 | use constant FALSE => not TRUE; | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | use base 'Mail::SPF::Test::Base'; | ||
| 27 | |||
| 28 | sub new { | ||
| 29 | my ($class, @options) = @_; | ||
| 30 | my $self = $class->SUPER::new(@options); | ||
| 31 | ... | ||
| 32 | return $self; | ||
| 33 | } | ||
| 34 | |||
| 35 | =head1 DESCRIPTION | ||
| 36 | |||
| 37 | B<Mail::SPF::Test::Base> is a common base class for all B<Mail::SPF::Test> | ||
| 38 | classes. | ||
| 39 | |||
| 40 | =head2 Constructor | ||
| 41 | |||
| 42 | The following constructor is provided: | ||
| 43 | |||
| 44 | =over | ||
| 45 | |||
| 46 | =item B<new(%options)>: returns I<Mail::SPF::Test::Base> | ||
| 47 | |||
| 48 | Creates a new object of the class on which the constructor was invoked. The | ||
| 49 | provided options are stored as key/value pairs in the new object. | ||
| 50 | |||
| 51 | The C<new> constructor may also be called on an object, in which case the | ||
| 52 | object is cloned. Any options provided override those from the old object. | ||
| 53 | |||
| 54 | There are no common options defined in B<Mail::SPF::Test::Base>. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub new { | ||
| 59 | my ($self, %options) = @_; | ||
| 60 | my $new = | ||
| 61 | ref($self) ? # Was new() invoked on a class or an object? | ||
| 62 | { %$self, %options } # Object: clone source object, override fields. | ||
| 63 | : \%options; # Class: create new object. | ||
| 64 | return bless($new, $self->class); | ||
| 65 | } | ||
| 66 | |||
| 67 | =back | ||
| 68 | |||
| 69 | =head2 Class methods | ||
| 70 | |||
| 71 | The following class methods are provided: | ||
| 72 | |||
| 73 | =over | ||
| 74 | |||
| 75 | =item B<class>: returns I<string> | ||
| 76 | |||
| 77 | Returns the class name of the class or object on which it is invoked. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub class { | ||
| 82 | my ($self) = @_; | ||
| 83 | return ref($self) || $self; | ||
| 84 | } | ||
| 85 | |||
| 86 | =back | ||
| 87 | |||
| 88 | =head2 Class methods | ||
| 89 | |||
| 90 | The following class methods are provided: | ||
| 91 | |||
| 92 | =over | ||
| 93 | |||
| 94 | =item B<make_accessor($name, $readonly)>: returns I<code-ref> | ||
| 95 | |||
| 96 | Creates an accessor method in the class on which it is invoked. The accessor | ||
| 97 | has the given name and accesses the object field of the same name. If | ||
| 98 | $readonly is B<true>, the accessor is made read-only. | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub make_accessor { | ||
| 103 | my ($class, $name, $readonly) = @_; | ||
| 104 | not ref($class) | ||
| 105 | or die('Class method called as an instance method'); | ||
| 106 | my $accessor_name = "${class}::${name}"; | ||
| 107 | my $accessor; | ||
| 108 | if ($readonly) { | ||
| 109 | $accessor = sub { | ||
| 110 | local *__ANON__ = $accessor_name; | ||
| 111 | my ($self, @value) = @_; | ||
| 112 | ref($self) | ||
| 113 | or die('Instance method called as a class method'); | ||
| 114 | not @value | ||
| 115 | or die("$accessor_name is read-only"); | ||
| 116 | return $self->{$name}; | ||
| 117 | }; | ||
| 118 | } | ||
| 119 | else { | ||
| 120 | $accessor = sub { | ||
| 121 | local *__ANON__ = $accessor_name; | ||
| 122 | my ($self, @value) = @_; | ||
| 123 | ref($self) | ||
| 124 | or die('Instance method called as a class method'); | ||
| 125 | $self->{$name} = $value[0] if @value; | ||
| 126 | return $self->{$name}; | ||
| 127 | }; | ||
| 128 | } | ||
| 129 | { | ||
| 130 | no strict 'refs'; | ||
| 131 | *{$accessor_name} = $accessor; | ||
| 132 | } | ||
| 133 | return $accessor; | ||
| 134 | } | ||
| 135 | |||
| 136 | =back | ||
| 137 | |||
| 138 | =head2 Instance methods | ||
| 139 | |||
| 140 | There are no common instance methods defined in B<Mail::SPF::Test::Base>. | ||
| 141 | |||
| 142 | =head1 SEE ALSO | ||
| 143 | |||
| 144 | L<Mail::SPF::Test> | ||
| 145 | |||
| 146 | For availability, support, and license information, see the README file | ||
| 147 | included with Mail::SPF::Test. | ||
| 148 | |||
| 149 | =head1 AUTHORS | ||
| 150 | |||
| 151 | Julian Mehnle <julian@mehnle.net> | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | TRUE; |