File Coverage

blib/lib/MOP/Slot.pm
Criterion Covered Total %
statement 39 39 100.0
branch 13 14 92.8
condition 7 9 77.7
subroutine 11 11 100.0
pod 6 6 100.0
total 76 79 96.2


line stmt bran cond sub pod time code
1             package MOP::Slot;
2             # ABSTRACT: A representation of a class slot
3              
4 30     30   118838 use strict;
  30         62  
  30         708  
5 30     30   115 use warnings;
  30         41  
  30         648  
6              
7 30     30   127 use Carp ();
  30         47  
  30         454  
8              
9 30     30   831 use MOP::Internal::Util;
  30         52  
  30         1329  
10              
11             our $VERSION = '0.14';
12             our $AUTHORITY = 'cpan:STEVAN';
13              
14 30     30   150 use parent 'UNIVERSAL::Object::Immutable';
  30         56  
  30         145  
15              
16             sub BUILDARGS {
17 72     72 1 12332 my $class = shift;
18 72         94 my $args;
19              
20 72 100 66     210 if ( scalar( @_ ) eq 2 && !(ref $_[0]) && ref $_[1] eq 'CODE' ) {
      66        
21 1         3 $args = +{ name => $_[0], initializer => $_[1] };
22             }
23             else {
24 71         156 $args = $class->SUPER::BUILDARGS( @_ );
25             }
26              
27             Carp::confess('[ARGS] You must specify a slot name')
28 72 100       815 unless $args->{name};
29             Carp::confess('[ARGS] You must specify a slot initializer')
30 71 100       223 unless $args->{initializer};
31             Carp::confess('[ARGS] The initializer specified must be a CODE reference')
32             unless ref $args->{initializer} eq 'CODE'
33 70 100 100     172 || MOP::Internal::Util::CAN_COERCE_TO_CODE_REF( $args->{initializer} );
34              
35 69         106 return $args;
36             }
37              
38             sub CREATE {
39 69     69 1 619 my ($class, $args) = @_;
40             # NOTE:
41             # Ideally this instance would actually just be
42             # a reference to an HE (C-level hash entry struct)
43             # but that is not something that is exposed at
44             # the language level. Instead we use an ARRAY
45             # ref to both 1) save space and 2) retain an
46             # illusion of opacity regarding these instances.
47             # - SL
48 69         179 return +[ $args->{name}, $args->{initializer} ]
49             }
50              
51             sub name {
52 16     16 1 8339 my ($self) = @_;
53 16         55 return $self->[0];
54             }
55              
56             sub initializer {
57 110     110 1 2023 my ($self) = @_;
58             return MOP::Internal::Util::CAN_COERCE_TO_CODE_REF( $self->[1] )
59 110 100       237 ? \&{ $self->[1] }
  18         54  
60             : $self->[1];
61             }
62              
63             sub origin_stash {
64 91     91 1 539 my ($self) = @_;
65             # NOTE:
66             # for the time being we are going to stick with
67             # the COMP_STASH as the indicator for the initalizers
68             # instead of the glob ref, which might be trickier
69             # however I really don't know, so time will tell.
70             # - SL
71 91         143 return MOP::Internal::Util::GET_STASH_NAME( $self->initializer );
72             }
73              
74             sub was_aliased_from {
75 18     18 1 44 my ($self, @classnames) = @_;
76              
77 18 50       51 Carp::confess('[ARGS] You must specify at least one classname')
78             if scalar( @classnames ) == 0;
79              
80 18         39 my $class = $self->origin_stash;
81 18         35 foreach my $candidate ( @classnames ) {
82 18 100       87 return 1 if $candidate eq $class;
83             }
84 3         14 return 0;
85             }
86              
87             1;
88              
89             __END__