File Coverage

blib/lib/Attribute/Overload.pm
Criterion Covered Total %
statement 37 38 97.3
branch 7 14 50.0
condition 1 3 33.3
subroutine 8 8 100.0
pod 0 1 0.0
total 53 64 82.8


line stmt bran cond sub pod time code
1 1     1   461 use 5.008;
  1         3  
  1         30  
2 1     1   4 use warnings;
  1         1  
  1         18  
3 1     1   3 use strict;
  1         2  
  1         43  
4              
5             package Attribute::Overload;
6             our $VERSION = '1.100710';
7             # ABSTRACT: Attribute that makes overloading easier
8              
9 1     1   6503 use Attribute::Handlers;
  1         6214  
  1         5  
10              
11             # RAWDATA to get '""' as such;
12             # CODE would interpret it as empty string and return nothing
13             sub UNIVERSAL::Overload : ATTR(CODE,RAWDATA) {
14 3     3 0 26303 my ($pkg, $symbol, $data) = @_[ 0, 1, 4 ];
15 3         5 our %overload;
16 3 50       12 for (ref $data eq 'ARRAY' ? @$data : $data) {
17 3 50       13 die "Too late to overload constant $_ in CHECK for $symbol\n"
18             if /^(integer|float|binary|qr?)$/;
19 3         7 s!\"\"!""!g;
20 3         4 $overload{$pkg}{$_} = *{$symbol}{NAME};
  3         17  
21             }
22 1     1   182 }
  1         2  
  1         4  
23              
24             sub INIT {
25              
26             # only eval here, because multiple overloaded subs must only
27             # trigger one 'use overload' statement
28 1     1   31 our %overload;
29 1         3 my $code;
30 1         7 while (my ($pkg, $pkgdef) = each %overload) {
31 1         2 my (@code, @constcode);
32 1         10 while (my ($op, $sub) = each %$pkgdef) {
33 3 50       14 if ($op =~ /^(integer|float|binary|qr?)$/) {
34 0         0 push @constcode => "$op => \\&$sub";
35             } else {
36 3         18 push @code => "'$op' => \\&$sub";
37             }
38             }
39 1 50 33     5 next unless @code || @constcode; # huh? no defs?
40 1         3 $code .= "package $pkg;\n";
41 1 50       12 $code .= "use overload\n" . join(",\n" => @code) . ";\n"
42             if @code;
43              
44             # Note: the following doesn't do anything, since import()
45             # is called at BEGIN via use(), but attributes are only
46             # evaluated during CHECK. So it's commented out for now.
47             # $code .= "BEGIN { sub import { overload::constant (\n" .
48             # join(",\n" => @constcode) . ")}};\n" if @constcode;
49             }
50              
51 1 50   1   10 eval $code if $code;
  1         2  
  1         13  
  1         145  
52 1 50       70 die $@ if $@;
53             }
54             1;
55              
56              
57             __END__