| 1 | use 5.008; | ||||||||||||||||||||||||||||
| 2 | use warnings; | ||||||||||||||||||||||||||||
| 3 | use strict; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Attribute::Overload; | ||||||||||||||||||||||||||||
| 6 | our $VERSION = '1.100710'; | ||||||||||||||||||||||||||||
| 7 | # ABSTRACT: Attribute that makes overloading easier | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | use Attribute::Handlers; | ||||||||||||||||||||||||||||
| 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 | 1 | ▶ | my ($pkg, $symbol, $data) = @_[ 0, 1, 4 ]; | ||||||||||||||||||||||||||
|
Subroutine
Overload: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 1 | our %overload; | |||||||||||||||||||||||||||
| 16 | 1 | ▶ | for (ref $data eq 'ARRAY' ? @$data : $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | die "Too late to overload constant $_ in CHECK for $symbol\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | if /^(integer|float|binary|qr?)$/; | ||||||||||||||||||||||||||||
| 19 | 1 | s!\"\"!""!g; | |||||||||||||||||||||||||||
| 20 | 1 | $overload{$pkg}{$_} = *{$symbol}{NAME}; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub INIT { | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # only eval here, because multiple overloaded subs must only | ||||||||||||||||||||||||||||
| 27 | # trigger one 'use overload' statement | ||||||||||||||||||||||||||||
| 28 | 1 | ▶ | our %overload; | ||||||||||||||||||||||||||
|
Subroutine
INIT: called CC=9 SCAR=23.1
| |||||||||||||||||||||||||||||
| 29 | 1 | my $code; | |||||||||||||||||||||||||||
| 30 | 1 | ▶ | while (my ($pkg, $pkgdef) = each %overload) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 1 | my (@code, @constcode); | |||||||||||||||||||||||||||
| 32 | 1 | ▶ | while (my ($op, $sub) = each %$pkgdef) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | if ($op =~ /^(integer|float|binary|qr?)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 1 | push @constcode => "$op => \\&$sub"; | |||||||||||||||||||||||||||
| 35 | } else { | ||||||||||||||||||||||||||||
| 36 | 1 | push @code => "'$op' => \\&$sub"; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 1 | ▶ | next unless @code || @constcode; # huh? no defs? | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@code or @constcode
MC/DC0%0/2
@code or @constcode
@code
@constcode
Truth table33%1/3
A or B
A= @codeB= @constcode
| |||||||||||||||||||||||||||||
| 40 | 1 | $code .= "package $pkg;\n"; | |||||||||||||||||||||||||||
| 41 | 1 | ▶ | $code .= "use overload\n" . join(",\n" => @code) . ";\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | eval $code if $code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 1; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | __END__ | ||||||||||||||||||||||||||||