/usr/share/perl5/FCM/Class/HASH.pm is in fcm 2016.12.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | #-------------------------------------------------------------------------------
# (C) British Crown Copyright 2006-16 Met Office.
#
# This file is part of FCM, tools for managing and building source code.
#
# FCM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FCM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FCM. If not, see <http://www.gnu.org/licenses/>.
#-------------------------------------------------------------------------------
use strict;
use warnings;
#-------------------------------------------------------------------------------
package FCM::Class::HASH;
use FCM::Class::Exception;
use Scalar::Util qw{reftype};
# Methods for working out the default value of an attribute.
my %ATTRIB_DEFAULT_BY = (
default => sub {
my $opt_ref = shift();
my $ret = $opt_ref->{default};
return (ref($ret) && reftype($ret) eq 'CODE' ? $ret->() : $ret);
},
isa => sub {
my $opt_ref = shift();
return
$opt_ref->{isa} eq 'ARRAY' ? []
: $opt_ref->{isa} eq 'HASH' ? {}
: $opt_ref->{isa} eq 'CODE' ? sub {}
: undef
;
},
);
# Checks the value of an attribute.
my $ATTRIB_CHECK = sub {
my ($class, $opt_ref, $key, $value, $caller_ref) = @_;
# Note: undef is always OK?
if (!defined($value)) {
return;
}
my $expected_isa = $opt_ref->{isa};
if (!$expected_isa || $expected_isa eq 'SCALAR' && !ref($value)) {
return;
}
if (!UNIVERSAL::isa($value, $expected_isa)) {
return FCM::Class::Exception->throw({
'code' => FCM::Class::Exception->CODE_TYPE,
'caller' => $caller_ref,
'package' => $class,
'key' => $key,
'type' => $expected_isa,
'value' => $value,
});
}
};
# Creates the methods of the class.
sub class {
my ($class, $attrib_opt_ref, $class_opt_ref) = @_;
my %class_opt = (
init => sub {},
init_attrib => sub {@_},
(defined($class_opt_ref) ? %{$class_opt_ref} : ()),
);
if (!defined($attrib_opt_ref)) {
$attrib_opt_ref = {};
}
my %attrib_opt;
while (my ($key, $item) = each(%{$attrib_opt_ref})) {
my %option = (
r => 1, # readable?
w => 1, # writable?
add => undef, # isa eq 'HASH' only, class of HASH element
default => undef, # default value or CODE to return it
isa => undef, # attribute type
( defined($item) && ref($item) ? %{$item}
: defined($item) ? (isa => $item)
: ()
),
);
if (defined($option{isa})) {
$option{isa}
= $option{isa} eq '$' ? 'SCALAR'
: $option{isa} eq '@' ? 'ARRAY'
: $option{isa} eq '%' ? 'HASH'
: $option{isa} eq '&' ? 'CODE'
: $option{isa} eq '*' ? 'GLOB'
: $option{isa}
;
}
$attrib_opt{$key} = \%option;
}
no strict qw{refs};
# $class->new(\%attrib)
*{$class . '::new'} = sub {
my $class = shift();
my ($attrib_ref) = $class_opt{init_attrib}->(@_);
my $caller_ref = [caller()];
my %attrib = (defined($attrib_ref) ? %{$attrib_ref} : ());
while (my ($key, $value) = each(%attrib)) {
$ATTRIB_CHECK->($class, $attrib_opt{$key}, $key, $value, $caller_ref);
}
my $self = bless(\%attrib, $class);
KEY:
while (my ($key, $opt_ref) = each(%attrib_opt)) {
if (exists($self->{$key})) {
next KEY;
}
for my $opt_name (qw{default isa}) {
if (defined($opt_ref->{$opt_name})) {
$self->{$key} = $ATTRIB_DEFAULT_BY{$opt_name}->($opt_ref);
next KEY;
}
}
}
$class_opt{init}->($self);
return $self;
};
# $instance->$methods()
while (my ($key, $opt_ref) = each(%attrib_opt)) {
# $instance->get_$attrib()
# $instance->get_$attrib($name)
if ($opt_ref->{r}) {
*{$class . '::get_' . $key}
= defined($opt_ref->{isa}) && $opt_ref->{isa} eq 'HASH'
? sub {
my ($self, $name) = @_;
if (!defined($name)) {
return $self->{$key};
}
if (exists($self->{$key}{$name})) {
return $self->{$key}{$name};
}
return;
}
: sub {$_[0]->{$key}}
;
}
# $instance->set_$attrib($value)
if ($opt_ref->{w}) {
*{$class . '::set_' . $key} = sub {
my ($self, $value) = @_;
$ATTRIB_CHECK->(
$class, $attrib_opt{$key}, $key, $value, [caller()],
);
$self->{$key} = $value;
return $self;
};
}
# $instance->add_$attrib($name,\%option)
if ( defined($opt_ref->{isa}) && $opt_ref->{isa} eq 'HASH'
&& defined($opt_ref->{add})
) {
*{$class . '::add_' . $key} = sub {
my ($self, $name, @args) = @_;
if (defined($self->{$key}{$name})) {
return $self->{$key}{$name};
}
$self->{$key}{$name} = $opt_ref->{add}->new(@args);
};
}
}
return 1;
}
#-------------------------------------------------------------------------------
1;
__END__
=head1 NAME
FCM::Class::HASH
=head1 SYNOPSIS
package Breakfast;
use base qw{FCM::Class::HASH};
__PACKAGE__->class(
{
eggs => {isa => '@'},
ham => {isa => '%'},
bacon => '$',
# ...
},
);
# Some time later...
$breakfast = Breakfast->new(\%attrib);
@eggs = @{$breakfast->get_eggs()};
$breakfast->set_ham(\%ham);
=head1 DESCRIPTION
Provides a simple method to create HASH-based classes.
The class() method creates the new() method for initiating a new instance. It
also provides a get_$attrib() and set_$attrib() accessors for each attribute.
Basic type checkings are performed on writing to the attributes to ensure
correct usage.
=head1 METHODS
=over 4
=item $class->class(\%attrib_opt,\%class_opt)
Creates the class, using the attribute options in %attrib_opt and %class_opt.
The %attrib_opt is used to configure the attributes of an instance of the class.
The key of each element is the name of the attribute, and the value is a HASH
containing the options of the attribute, or a SCALAR. (If a SCALAR is specified,
it is equivalent to {isa => value}.). The options may contain:
=over 4
=item r
(Default=true) If true, the attribute is readable.
=item w
(Default=true) If true, the attribute is writable.
=item add
(Default=undef) This is only useful for a HASH attribute. If defined, it should
be the name of a class (e.g. $attrib_class). The HASH attribute will receive an
extra method $instance->add_$attrib($key,@args). The method will assign the
$name element of the HASH attribute to the result of $attrib_class->new(@args).
=item default
(Default=undef) The default value of the attribute.
If this option is defined, the attribute will be initialised to the specified
value when the new() method is called. In the special case where the value of
this option is a CODE reference, it will be invoked as $code->(\%attrib), and
the default value will be the returned value of the CODE reference. This is
useful, for example, if the default value needs to be a new instance of a class.
If a genuine CODE reference is required as the default, this option should be
set to a CODE reference that returns the required CODE reference itself.
For example:
Foo->class({
foo => {default => 'foo'}, # 'foo'
bar => {default => sub {get_id()}}, # the next id
baz => {default => sub {\&code}}, # &code
});
{
my $id = 0;
sub get_id {$id++}
}
If the default options is not defined, and if the attribute "isa" is ARRAY, HASH
or CODE, then the default value is [], {} and sub {} respectively.
=item isa
(Default=undef) The expected type of the attribute. If this optioin is defined
as $type, a new $value of the attribute is only accepted if $value is undef,
UNIVERSAL::isa($value,$type) returns true or if $type is C<SCALAR> and the new
value is not a reference.
The attribute accepts $, @, %, & and * as aliases to SCALAR, ARRAY, HASH, CODE
and GLOB.
=back
The argument %class_opt can have the following elements:
=over 4
=item init
If $class_opt{init} is defined, it should be a CODE reference. If specified, it
will be called just after the instance is blessed in the $class->new() method,
with an interface $f->($instance) where $instance is the new instance.
=item init_attrib
The value of this option must be a CODE. The $class->new() normally expects a
single HASH reference argument. If an alternate interface to the $class->new()
is required, this CODE can be used to turn the input argument list to the
expected HASH reference.
=back
=item $class->new(\%attrib)
Creates a new instance with %attrib. Initial values of the attributes can be
specified using %attrib. Otherwise, the method will attempt to assign the
default values, as specified in the class() method, to the newly created
instance.
=item $instance->get_$attrib()
Returns a readable attribute.
=item $instance->get_$attrib($key)
These are available for HASH attributes only. Returns the value of an element in
a readable attribute.
=item $instance->set_$attrib($value)
Sets the value of a writable attribute. Returns $instance.
=item $instance->add_$attrib($key,@args)
These are available for HASH attributes (with the C<add> attribute option
defined) only. Adds a new $key element to the HASH attribute. Returns the newly
added element.
=back
=head1 DIAGNOSTICS
L<FCM::Class::Exception|FCM::Class::Exception> is thrown on error.
=head1 SEE ALSO
Inspired by the standard module L<Class::Struct|Class::Struct> and CPAN modules
such as L<Class::Accessor|Class::Accessor>.
=head1 COPYRIGHT
(C) Crown copyright Met Office. All rights reserved.
=cut
|