/usr/share/perl5/Class/Meta/Constructor.pm is in libclass-meta-perl 0.66-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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | package Class::Meta::Constructor;
=head1 NAME
Class::Meta::Constructor - Class::Meta class constructor introspection
=head1 SYNOPSIS
# Assuming MyApp::Thingy was generated by Class::Meta.
my $class = MyApp::Thingy->my_class;
print "\nConstructors:\n";
for my $ctor ($class->constructors) {
print " o ", $ctor->name, $/;
my $thingy = $ctor->call($class->package);
}
=head1 DESCRIPTION
This class provides an interface to the C<Class::Meta> objects that describe
class constructors. It supports a simple description of the constructor, a
label, and the constructor visibility (private, protected, trusted,or public).
Class::Meta::Constructor objects are created by Class::Meta; they are never
instantiated directly in client code. To access the constructor objects for a
Class::Meta-generated class, simply call its C<my_class()> method to retrieve
its Class::Meta::Class object, and then call the C<constructors()> method on
the Class::Meta::Class object.
=cut
##############################################################################
# Dependencies #
##############################################################################
use strict;
##############################################################################
# Package Globals #
##############################################################################
our $VERSION = '0.66';
##############################################################################
# Constructors #
##############################################################################
=head1 INTERFACE
=head2 Constructors
=head3 new
A protected method for constructing a Class::Meta::Constructor object. Do not
call this method directly; Call the
L<C<add_constructor()>|Class::Meta/"add_constructor"> method on a Class::Meta
object, instead.
=cut
sub new {
my $pkg = shift;
my $class = shift;
# Check to make sure that only Class::Meta or a subclass is constructing a
# Class::Meta::Constructor object.
my $caller = caller;
Class::Meta->handle_error("Package '$caller' cannot create $pkg "
. "objects")
unless UNIVERSAL::isa($caller, 'Class::Meta')
|| UNIVERSAL::isa($caller, __PACKAGE__);
# Make sure we can get all the arguments.
$class->handle_error("Odd number of parameters in call to new() when "
. "named parameters were expected")
if @_ % 2;
my %p = @_;
# Validate the name.
$class->handle_error("Parameter 'name' is required in call to new()")
unless $p{name};
$class->handle_error("Constructor '$p{name}' is not a valid constructor "
. "name -- only alphanumeric and '_' characters "
. "allowed")
if $p{name} =~ /\W/;
# Make sure the name hasn't already been used for another constructor or
# method.
$class->handle_error("Method '$p{name}' already exists in class "
. "'$class->{package}'")
if exists $class->{ctors}{$p{name}}
or exists $class->{meths}{$p{name}};
# Check the visibility.
if (exists $p{view}) {
$p{view} = Class::Meta::_str_to_const($p{view});
$class->handle_error("Not a valid view parameter: '$p{view}'")
unless $p{view} == Class::Meta::PUBLIC
|| $p{view} == Class::Meta::PROTECTED
|| $p{view} == Class::Meta::TRUSTED
|| $p{view} == Class::Meta::PRIVATE;
} else {
# Make it public by default.
$p{view} = Class::Meta::PUBLIC;
}
# Use passed code or create the constructor?
if ($p{code}) {
my $ref = ref $p{code};
$class->handle_error(
'Parameter code must be a code reference'
) unless $ref && $ref eq 'CODE';
$p{create} = 0;
} else {
$p{create} = 1 unless exists $p{create};
}
# Validate or create the method caller if necessary.
if ($p{caller}) {
my $ref = ref $p{caller};
$class->handle_error("Parameter caller must be a code reference")
unless $ref && $ref eq 'CODE';
} else {
$p{caller} = UNIVERSAL::can($class->{package}, $p{name})
unless $p{create};
}
# Create and cache the constructor object.
$p{package} = $class->{package};
$class->{ctors}{$p{name}} = bless \%p, ref $pkg || $pkg;
# Index its view.
push @{ $class->{all_ctor_ord} }, $p{name};
if ($p{view} > Class::Meta::PRIVATE) {
push @{$class->{prot_ctor_ord}}, $p{name}
unless $p{view} == Class::Meta::TRUSTED;
if ($p{view} > Class::Meta::PROTECTED) {
push @{$class->{trst_ctor_ord}}, $p{name};
push @{$class->{ctor_ord}}, $p{name}
if $p{view} == Class::Meta::PUBLIC;
}
}
# Store a reference to the class object.
$p{class} = $class;
# Let 'em have it.
return $class->{ctors}{$p{name}};
}
##############################################################################
# Instance Methods #
##############################################################################
=head2 Instance Methods
=head3 name
my $name = $ctor->name;
Returns the constructor name.
=head3 package
my $package = $ctor->package;
Returns the package name of the class that constructor is associated with.
=head3 desc
my $desc = $ctor->desc;
Returns the description of the constructor.
=head3 label
my $desc = $ctor->label;
Returns label for the constructor.
=head3 view
my $view = $ctor->view;
Returns the view of the constructor, reflecting its visibility. The possible
values are defined by the following constants:
=over 4
=item Class::Meta::PUBLIC
=item Class::Meta::PRIVATE
=item Class::Meta::TRUSTED
=item Class::Meta::PROTECTED
=back
=head3 class
my $class = $ctor->class;
Returns the Class::Meta::Class object that this constructor is associated
with. Note that this object will always represent the class in which the
constructor is defined, and I<not> any of its subclasses.
=cut
sub name { $_[0]->{name} }
sub package { $_[0]->{package} }
sub desc { $_[0]->{desc} }
sub label { $_[0]->{label} }
sub view { $_[0]->{view} }
sub class { $_[0]->{class} }
=head3 call
my $obj = $ctor->call($package, @params);
Executes the constructor. Pass in the name of the class for which it is being
executed (since, thanks to subclassing, it may be different than the class
with which the constructor is associated). All other parameters will be passed
to the constructor. Note that it uses a C<goto> to execute the constructor, so
the call to C<call()> itself will not appear in a call stack trace.
=cut
sub call {
my $self = shift;
my $code = $self->{caller} or $self->class->handle_error(
q{Cannot call constructor '}, $self->name, q{'}
);
goto &$code;
}
##############################################################################
=head3 build
$ctor->build($class);
This is a protected method, designed to be called only by the Class::Meta
class or a subclass of Class::Meta. It takes a single argument, the
Class::Meta::Class object for the class in which the constructor was defined,
and generates constructor method for the Class::Meta::Constructor, either by
installing the code reference passed in the C<code> parameter or by creating
the constructor from scratch.
Although you should never call this method directly, subclasses of
Class::Meta::Constructor may need to override its behavior.
=cut
sub build {
my ($self, $specs) = @_;
# Check to make sure that only Class::Meta or a subclass is building
# constructors.
my $caller = caller;
$self->class->handle_error("Package '$caller' cannot call " . ref($self)
. "->build")
unless UNIVERSAL::isa($caller, 'Class::Meta')
|| UNIVERSAL::isa($caller, __PACKAGE__);
# Just bail if we're not creating or installing the constructor.
return $self unless delete $self->{create} || $self->{code};
# Build a construtor that takes a parameter list and assigns the
# the values to the appropriate attributes.
my $name = $self->name;
my $sub = delete $self->{code} || sub {
my $package = ref $_[0] ? ref shift : shift;
my $class = $specs->{$package};
# Throw an exception for attempts to create items of an abstract
# class.
$class->handle_error(
"Cannot construct objects of astract class $package"
) if $class->abstract;
# Is there a sub passed as the last argument?
my $sub = @_ % 2 && ref $_[-1] eq 'CODE' ? pop @_ : undef;
# Just grab the parameters and let an error be thrown by Perl
# if there aren't the right number of them.
my %p = @_;
my $new = bless {} => $package;
# Assign all of the attribute values.
my @req;
if (my $attrs = $class->{attrs}) {
foreach my $attr (@{ $attrs }{ @{ $class->{all_attr_ord} } }) {
# Skip class attributes.
next if $attr->context == Class::Meta::CLASS;
my $key = $attr->name;
if (exists $p{$key} && $attr->authz >= Class::Meta::SET) {
# Let them set the value.
$attr->set($new, delete $p{$key});
} elsif (!exists $new->{$key}) {
# Use the default value.
$new->{$key} = $attr->default;
push @req, $attr if $attr->required;
}
}
}
# Check for params for which attributes are private or don't exist.
if (my @attributes = keys %p) {
# Attempts to assign to non-existent attributes fail.
my $c = $#attributes > 0 ? 'attributes' : 'attribute';
local $" = q{', '};
$class->handle_error(
"No such $c '@attributes' in $self->{package} objects"
);
}
# Run the block passed, if there is one.
$sub->($new) if $sub;
# Enforce required attributes.
if (@req and my @miss = grep { !defined $new->{ $_->name } } @req ) {
my $c = $#miss > 0 ? 'Attributes' : 'Attribute';
my $a = join q{', '}, map { $_->name } @miss;
$class->handle_error(
"$c '$a' must be defined in $self->{package} objects"
);
}
return $new;
};
# Add protected, private, or trusted checks, if required.
if ($self->view == Class::Meta::PROTECTED) {
my $real_sub = $sub;
my $pkg = $self->package;
my $class = $self->class;
$sub = sub {
$class->handle_error("$name is a protected constrctor of $pkg")
unless caller->isa($pkg);
goto &$real_sub;
};
} elsif ($self->view == Class::Meta::PRIVATE) {
my $real_sub = $sub;
my $pkg = $self->package;
my $class = $self->class;
$sub = sub {
$class->handle_error("$name is a private constructor of $pkg")
unless caller eq $pkg;
goto &$real_sub;
};
}
# Install the constructor.
$self->{caller} ||= $sub;
no strict 'refs';
*{"$self->{package}::$name"} = $sub;
}
1;
__END__
=head1 BUGS
Please send bug reports to <bug-class-meta@rt.cpan.org> or report them via the
CPAN Request Tracker at L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-Meta>.
=head1 SUPPORT
This module is stored in an open L<GitHub
repository|http://github.com/theory/class-meta/>. Feel free to fork and
contribute!
Please file bug reports via L<GitHub
Issues|http://github.com/theory/class-meta/issues/> or by sending mail to
L<bug-Class-Meta@rt.cpan.org|mailto:bug-Class-Meta@rt.cpan.org>.
=head1 AUTHOR
David E. Wheeler <david@justatheory.com>
=head1 SEE ALSO
Other classes of interest within the Class::Meta distribution include:
=over 4
=item L<Class::Meta|Class::Meta>
=item L<Class::Meta::Class|Class::Meta::Class>
=item L<Class::Meta::Method|Class::Meta::Method>
=item L<Class::Meta::Attribute|Class::Meta::Attribute>
=back
=head1 COPYRIGHT AND LICENSE
Copyright (c) 2002-2011, David E. Wheeler. Some Rights Reserved.
This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|