File Coverage

blib/lib/Perl6/Currying.pm
Criterion Covered Total %
statement 31 32 96.8
branch 3 6 50.0
condition 4 6 66.6
subroutine 5 5 100.0
pod 1 1 100.0
total 44 50 88.0


line stmt bran cond sub pod time code
1             package Perl6::Currying;
2              
3 1     1   7351 use Filter::Simple;
  1         67791  
  1         8  
4 1     1   56 use Carp;
  1         2  
  1         3981  
5              
6             croak "Perl6::Placeholders should not be loaded before Perl6::Currying"
7             if $INC{'Perl6/Placeholders.pm'};
8              
9             my $name = qr/(?:\w+(?:::\w+)*)/;
10             my $scalar = qr/\s*\$\w+\s*/;
11             our $balbrack = qr{ (?: (?> [^{}]+ ) | \{ (??{ $balbrack }) \} )* }x;
12              
13             sub prebind {
14 4     4 1 243 my $sub = shift;
15 4 50       13 croak "Odd list of bindings for prebind" if @_%2;
16 4         14 my %bound = @_;
17 4   66     21 my $proto = $prototype{$sub} ||= prototype($sub);
18 4 50       81 croak "Can't prebind sub with prototype ($proto)"
19             unless $proto =~ /^$scalar(,$scalar)*$/;
20 4         53 croak "Can't prebind nonexistent parameter \$$_ of sub($proto)"
21 4         12 foreach grep { $proto !~ /\$$_/ } keys %bound;
22 4   66     19 my $parampos = $parampos{$sub} ||= do {
23 2         13 my @params = $proto =~ /(\w+)/g;
24 2         4 my %parampos; @parampos{@params} = 0..$#params; \%parampos;
  2         9  
  2         11  
25             };
26 4         26 my @bound = sort { $b->{pos} <=> $a->{pos} }
  0         0  
27             map { pos=>$parampos->{$_}, val=>$bound{$_}}, keys %bound;
28 4         89 $proto =~ s/,?\$$_// for keys %bound;
29             my $HOF = sub {
30 4     4   37 splice @_, $bound[$_]{pos}, 0, $bound[$_]{val} for 0..$#bound;
31 4         16 goto &$sub;
32 4         23 };
33 4         13 $prototype{$HOF} = $proto;
34 4         19 return $HOF;
35             }
36              
37             sub Perl6::Currying::Attributes::MODIFY_CODE_ATTRIBUTES {
38 1     1   5916 my( $package, $ref, @attrs) = @_;
39 1         6 for my $i (reverse 0..$#attrs) {
40 1 50       9 next unless $attrs[$i] =~ /^Prototype\((.*)\)$/;
41 1         5 $prototype{$ref} = $1;
42 1         6 splice @attrs, $i;
43             }
44 1         4 return @attrs;
45             }
46              
47             push @UNIVERSAL::ISA, 'Perl6::Currying::Attributes';
48              
49             FILTER_ONLY
50             executable => sub {
51             # Subroutine declarations
52             s
53             gx;
54             s
55             gx;
56             # Method call syntax
57             s{(&$name)\.prebind\(}
58             {Perl6::Currying::prebind(\\$1,}g;
59             s{&?(\$$name)\.prebind\(}
60             {Perl6::Currying::prebind($1,}g;
61             s{&\{($balbrack)\}\.prebind\(}
62             {Perl6::Currying::prebind($1,}g;
63             # Indirect object syntax
64             s[\bprebind\s*(&$name)\s*: ]
65             [Perl6::Currying::prebind \\$1,]g;
66             s[\bprebind\s*&?(\$$name)\s*:]
67             [Perl6::Currying::prebind $1,]g;
68             s[\bprebind\s*&\{($balbrack)\}\s*:]
69             [Perl6::Currying::prebind $1,]g;
70             },
71              
72             __END__