| 1 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | package Class::Accessor::Class 0.504; | ||||||||||||||||||||
| 4 | 3 | ▶ | use Class::Accessor 0.16 (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use parent 'Class::Accessor'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | # ABSTRACT: simple class variable accessors | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | #pod =head1 SYNOPSIS | ||||||||||||||||||||
| 9 | #pod | ||||||||||||||||||||
| 10 | #pod Set up a module with class accessors: | ||||||||||||||||||||
| 11 | #pod | ||||||||||||||||||||
| 12 | #pod package Text::Fortune; | ||||||||||||||||||||
| 13 | #pod | ||||||||||||||||||||
| 14 | #pod use base qw(Class::Accessor::Class Exporter); | ||||||||||||||||||||
| 15 | #pod Robot->mk_class_accessors(qw(language offensive collection)); | ||||||||||||||||||||
| 16 | #pod | ||||||||||||||||||||
| 17 | #pod sub fortune { | ||||||||||||||||||||
| 18 | #pod if (__PACKAGE__->offensive) { | ||||||||||||||||||||
| 19 | #pod .. | ||||||||||||||||||||
| 20 | #pod | ||||||||||||||||||||
| 21 | #pod Then, when using the module: | ||||||||||||||||||||
| 22 | #pod | ||||||||||||||||||||
| 23 | #pod use Text::Fortune; | ||||||||||||||||||||
| 24 | #pod | ||||||||||||||||||||
| 25 | #pod Text::Fortune->offensive(1); | ||||||||||||||||||||
| 26 | #pod | ||||||||||||||||||||
| 27 | #pod print fortune; # prints an offensive fortune | ||||||||||||||||||||
| 28 | #pod | ||||||||||||||||||||
| 29 | #pod Text::Fortune->language('EO'); | ||||||||||||||||||||
| 30 | #pod | ||||||||||||||||||||
| 31 | #pod print fortune; # prints an offensive fortune in Esperanto | ||||||||||||||||||||
| 32 | #pod | ||||||||||||||||||||
| 33 | #pod =head1 DESCRIPTION | ||||||||||||||||||||
| 34 | #pod | ||||||||||||||||||||
| 35 | #pod Class::Accessor::Class provides a simple way to create accessor and mutator | ||||||||||||||||||||
| 36 | #pod methods for class variables, just as Class::Accessor provides for objects. It | ||||||||||||||||||||
| 37 | #pod can use either an enclosed lexical variable, or a package variable. | ||||||||||||||||||||
| 38 | #pod | ||||||||||||||||||||
| 39 | #pod This module was once implemented in terms of Class::Accessor, but changes to | ||||||||||||||||||||
| 40 | #pod that module broke this relationship. Class::Accessor::Class is still a | ||||||||||||||||||||
| 41 | #pod subclass of Class::Accessor, strictly for historical reasons. As a side | ||||||||||||||||||||
| 42 | #pod benefit, a class that isa Class::Accessor::Class is also a Class::Accessor | ||||||||||||||||||||
| 43 | #pod and can use its methods. | ||||||||||||||||||||
| 44 | #pod | ||||||||||||||||||||
| 45 | #pod =method mk_class_accessors | ||||||||||||||||||||
| 46 | #pod | ||||||||||||||||||||
| 47 | #pod package Foo; | ||||||||||||||||||||
| 48 | #pod use base qw(Class::Accessor::Class); | ||||||||||||||||||||
| 49 | #pod Foo->mk_class_accessors(qw(foo bar baz)); | ||||||||||||||||||||
| 50 | #pod | ||||||||||||||||||||
| 51 | #pod Foo->foo(10); | ||||||||||||||||||||
| 52 | #pod my $obj = new Foo; | ||||||||||||||||||||
| 53 | #pod print $obj->foo; # 10 | ||||||||||||||||||||
| 54 | #pod | ||||||||||||||||||||
| 55 | #pod This method adds accessors for the named class variables. The accessor will | ||||||||||||||||||||
| 56 | #pod get or set a lexical variable to which the accessor is the only access. | ||||||||||||||||||||
| 57 | #pod | ||||||||||||||||||||
| 58 | #pod =cut | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 1 | ▶ | sub mk_class_accessors { | ||||||||||||||||||
|
Subroutine
mk_class_accessors: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 61 | 1 | my ($self, @fields) = @_; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | ## no critic (ProhibitNoStrict) | ||||||||||||||||||||
| 64 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 65 | 1 | for my $field (@fields) { | |||||||||||||||||||
| 66 | 1 | *{"${self}::$field"} = $self->make_class_accessor($field); | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | #pod =method mk_package_accessors | ||||||||||||||||||||
| 71 | #pod | ||||||||||||||||||||
| 72 | #pod package Foo; | ||||||||||||||||||||
| 73 | #pod use base qw(Class::Accessor::Class); | ||||||||||||||||||||
| 74 | #pod Foo->mk_package_accessors(qw(foo bar baz)); | ||||||||||||||||||||
| 75 | #pod | ||||||||||||||||||||
| 76 | #pod Foo->foo(10); | ||||||||||||||||||||
| 77 | #pod my $obj = new Foo; | ||||||||||||||||||||
| 78 | #pod print $obj->foo; # 10 | ||||||||||||||||||||
| 79 | #pod print $Foo::foo; # 10 | ||||||||||||||||||||
| 80 | #pod | ||||||||||||||||||||
| 81 | #pod This method adds accessors for the named class variables. The accessor will | ||||||||||||||||||||
| 82 | #pod get or set the named variable in the package's symbol table. | ||||||||||||||||||||
| 83 | #pod | ||||||||||||||||||||
| 84 | #pod =cut | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 1 | ▶ | sub mk_package_accessors { | ||||||||||||||||||
|
Subroutine
mk_package_accessors: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 87 | 1 | my ($self, @fields) = @_; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | ## no critic (ProhibitNoStrict) | ||||||||||||||||||||
| 90 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 91 | 1 | for my $field (@fields) { | |||||||||||||||||||
| 92 | 1 | *{"${self}::$field"} = $self->make_package_accessor($field); | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | #pod =head1 DETAILS | ||||||||||||||||||||
| 97 | #pod | ||||||||||||||||||||
| 98 | #pod =head2 make_class_accessor | ||||||||||||||||||||
| 99 | #pod | ||||||||||||||||||||
| 100 | #pod $accessor = Class->make_class_accessor($field); | ||||||||||||||||||||
| 101 | #pod | ||||||||||||||||||||
| 102 | #pod This method generates a subroutine reference which acts as an accessor for the | ||||||||||||||||||||
| 103 | #pod named field. | ||||||||||||||||||||
| 104 | #pod | ||||||||||||||||||||
| 105 | #pod =cut | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | { | ||||||||||||||||||||
| 108 | 3 | my %accessor; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 4 | ▶ | sub make_class_accessor { | ||||||||||||||||||
|
Subroutine
make_class_accessor: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 111 | 4 | my ($class, $field) = @_; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | return $accessor{$class}{$field} | ||||||||||||||||||||
| 114 | 4 | ▶ | if $accessor{$class}{$field}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 3 | my $field_value; | |||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 37 | ▶ | $accessor{$class}{$field} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 119 | 37 | my $class = shift; | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | return @_ | ||||||||||||||||||||
| 122 | 37 | ▶ | ? ($field_value = $_[0]) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | : $field_value; | ||||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | 3 | } | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | #pod =head2 make_package_accessor | ||||||||||||||||||||
| 129 | #pod | ||||||||||||||||||||
| 130 | #pod $accessor = Class->make_package_accessor($field); | ||||||||||||||||||||
| 131 | #pod | ||||||||||||||||||||
| 132 | #pod This method generates a subroutine reference which acts as an accessor for the | ||||||||||||||||||||
| 133 | #pod named field, which is stored in the scalar named C<field> in C<Class>'s symbol | ||||||||||||||||||||
| 134 | #pod table. | ||||||||||||||||||||
| 135 | #pod | ||||||||||||||||||||
| 136 | #pod This can be useful for dealing with legacy code, but using package variables is | ||||||||||||||||||||
| 137 | #pod almost never a good idea for new code. Use this with care. | ||||||||||||||||||||
| 138 | #pod | ||||||||||||||||||||
| 139 | #pod =cut | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 3 | ▶ | sub make_package_accessor { | ||||||||||||||||||
|
Subroutine
make_package_accessor: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 142 | 1 | my ($self, $field) = @_; | |||||||||||||||||||
| 143 | 1 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 1 | my $varname = "$class\:\:$field"; | |||||||||||||||||||
| 146 | 30 | ▶ | return sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 147 | 30 | my $class = shift; | |||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | ## no critic (ProhibitNoStrict) | ||||||||||||||||||||
| 150 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 151 | return @_ | ||||||||||||||||||||
| 152 | 5 | ? (${$varname} = $_[0]) | |||||||||||||||||||
| 153 | 30 | ▶ | : ${$varname} | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | 1 | } | |||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 3 | 1; | |||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||