File Coverage

blib/lib/MooseX/Role/WithOverloading/Meta/Role/Application.pm
Criterion Covered Total %
statement 9 29 31.0
branch 0 14 0.0
condition 0 3 0.0
subroutine 3 5 60.0
pod 1 1 100.0
total 13 52 25.0


line stmt bran cond sub pod time code
1             package MooseX::Role::WithOverloading::Meta::Role::Application;
2             # ABSTRACT: (DEPRECATED) Role application role for Roles which support overloading
3              
4             our $VERSION = '0.17';
5              
6 9     9   7066 use Moose::Role 1.15;
  9         221  
  9         61  
7 9     9   45418 use overload ();
  9         18  
  9         204  
8 9     9   52 use namespace::autoclean;
  9         18  
  9         65  
9              
10             requires 'apply_methods';
11              
12             #pod =method overload_ops
13             #pod
14             #pod Returns an arrayref of the names of overloaded operations
15             #pod
16             #pod =cut
17              
18             has overload_ops => (
19             is => 'ro',
20             isa => 'ArrayRef[Str]',
21             builder => '_build_overload_ops',
22             );
23              
24             sub _build_overload_ops {
25 0     0     return [map { split /\s+/ } values %overload::ops];
  0            
26             }
27              
28             #pod =method apply_methods ($role, $other)
29             #pod
30             #pod Wrapped with an after modifier which calls the C<< ->apply_overloading >>
31             #pod method.
32             #pod
33             #pod =cut
34              
35             after apply_methods => sub {
36             my ($self, $role, $other) = @_;
37             $self->apply_overloading($role, $other);
38             };
39              
40             #pod =method apply_overloading ($role, $other)
41             #pod
42             #pod Does the heavy lifting of applying overload operations to
43             #pod a class or role which the role is applied to.
44             #pod
45             #pod =cut
46              
47             sub apply_overloading {
48 0     0 1   my ($self, $role, $other) = @_;
49 0 0         return unless overload::Overloaded($role->name);
50              
51             # &(( indicates that overloading is turned on with Perl 5.18+. &() does
52             # this in earlier perls. $() stores the fallback value if one was set.
53 0           for my $sym (qw{ &(( &() $() }) {
54             # Simply checking ->has_package_symbol doesn't work. With 5.18+, a
55             # package may have &() and $() symbols but they may be undef.
56 0           my $ref = $role->get_package_symbol($sym);
57 0 0         $other->add_package_symbol($sym => $ref)
58             if defined $ref;
59             }
60              
61             # register with magic by touching (changes to SVf_AMAGIC removed %OVERLOAD in 5.17.0)
62 0 0         $other->get_or_add_package_symbol('%OVERLOAD')->{dummy}++ if $^V < 5.017000;
63              
64 0           for my $op (@{ $self->overload_ops }) {
  0            
65 0           my $code_sym = '&(' . $op;
66              
67 0 0         next if overload::Method($other->name, $op);
68 0 0         next unless $role->has_package_symbol($code_sym);
69              
70 0           my $meth = $role->get_package_symbol($code_sym);
71 0 0         next unless $meth;
72              
73             # when using "use overload $op => sub { };" this is the actual method
74             # to be called on overloading. otherwise it's \&overload::nil. see
75             # below.
76 0           $other->add_package_symbol($code_sym => $meth);
77              
78             # when using "use overload $op => 'method_name';" overload::nil is
79             # installed into the code slot of the glob and the actual method called
80             # is determined by the scalar slot of the same glob.
81 0 0 0       if ($meth == \&overload::nil || $meth == \&overload::_nil) {
82 0           my $scalar_sym = qq{\$($op};
83             $other->add_package_symbol(
84 0           $scalar_sym => ${ $role->get_package_symbol($scalar_sym) },
  0            
85             );
86             }
87             }
88             }
89              
90             1;
91              
92             __END__
93              
94             =pod
95              
96             =encoding UTF-8
97              
98             =head1 NAME
99              
100             MooseX::Role::WithOverloading::Meta::Role::Application - (DEPRECATED) Role application role for Roles which support overloading
101              
102             =head1 VERSION
103              
104             version 0.17
105              
106             =head1 METHODS
107              
108             =head2 overload_ops
109              
110             Returns an arrayref of the names of overloaded operations
111              
112             =head2 apply_methods ($role, $other)
113              
114             Wrapped with an after modifier which calls the C<< ->apply_overloading >>
115             method.
116              
117             =head2 apply_overloading ($role, $other)
118              
119             Does the heavy lifting of applying overload operations to
120             a class or role which the role is applied to.
121              
122             =head1 SUPPORT
123              
124             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Role-WithOverloading>
125             (or L<bug-MooseX-Role-WithOverloading@rt.cpan.org|mailto:bug-MooseX-Role-WithOverloading@rt.cpan.org>).
126              
127             There is also a mailing list available for users of this distribution, at
128             L<http://lists.perl.org/list/moose.html>.
129              
130             There is also an irc channel available for users of this distribution, at
131             irc://irc.perl.org/#moose.
132              
133             =head1 AUTHORS
134              
135             =over 4
136              
137             =item *
138              
139             Florian Ragwitz <rafl@debian.org>
140              
141             =item *
142              
143             Tomas Doran <bobtfish@bobtfish.net>
144              
145             =back
146              
147             =head1 COPYRIGHT AND LICENCE
148              
149             This software is copyright (c) 2009 by Florian Ragwitz.
150              
151             This is free software; you can redistribute it and/or modify it under
152             the same terms as the Perl 5 programming language system itself.
153              
154             =cut