File Coverage

blib/lib/MOP/Slot.pm
Criterion Covered Total %
statement 40 40 100.0
branch 13 14 92.8
condition 7 9 77.7
subroutine 12 12 100.0
pod 6 6 100.0
total 78 81 96.3


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