| 1 | package AutoCurry; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # Tom Moertel <tom@moertel.com> | ||||||||||||||||||||
| 4 | # 2005-02-17 | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | AutoCurry - automatically create currying variants of functions | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | use AutoCurry qw( foo ); # pass :all to curry all functions | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | sub foo { print "@_\n"; } | ||||||||||||||||||||
| 15 | # currying variant, foo_c, is created automatically | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | my $hello = foo_c("Hello, "); | ||||||||||||||||||||
| 18 | $hello->("world!"); # Hello, world! | ||||||||||||||||||||
| 19 | $hello->("Pittsburgh!"); # Hello, Pittsburgh! | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 2 | our $VERSION = "0.1003"; | |||||||||||||||||||
| 29 | 2 | our $suffix = "_c"; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 2 | my $PKG = __PACKAGE__; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 10 | ▶ | sub _debug { print STDERR "AutoCurry: @_\n" if $ENV{AUTOCURRY_DEBUG} } | ||||||||||||||||||
|
Subroutine
_debug: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 19 | ▶ | sub curry { | ||||||||||||||||||
|
Subroutine
curry: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 36 | 19 | my $f = shift; | |||||||||||||||||||
| 37 | 19 | my $args = \@_; | |||||||||||||||||||
| 38 | 19 | ▶ | sub { $f->(@$args, @_) }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 4 | ▶ | sub curry_package { | ||||||||||||||||||
|
Subroutine
curry_package: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 42 | 4 | ▶ | my $pkg = shift || caller; | ||||||||||||||||||
|
Condition66%2/3
shift() || caller
MC/DC0%0/2
shift() || caller
shift()
caller
Truth table66%2/3
A || B
A= shift()B= caller
| |||||||||||||||||||||
| 43 | 4 | curry_named_functions_from_package( $pkg, | |||||||||||||||||||
| 44 | get_function_names_from_package( $pkg ) | ||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 4 | ▶ | sub curry_named_functions { | ||||||||||||||||||
|
Subroutine
curry_named_functions: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 49 | 4 | return curry_named_functions_from_package( scalar caller(), @_ ); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 11 | ▶ | sub curry_named_functions_from_package { | ||||||||||||||||||
|
Subroutine
curry_named_functions_from_package: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 53 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 54 | 11 | my $pkg = shift() . "::"; | |||||||||||||||||||
| 55 | map { | ||||||||||||||||||||
| 56 | 10 | my $curried_name = $_ . $suffix; | |||||||||||||||||||
| 57 | carp "$PKG: currying $_ over existing $curried_name" | ||||||||||||||||||||
| 58 | 10 | ▶ | if *$curried_name{CODE}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 10 | _debug("making $curried_name"); | |||||||||||||||||||
| 60 | 10 | *$curried_name = curry( \&curry, \&$_ ); | |||||||||||||||||||
| 61 | 10 | $curried_name; | |||||||||||||||||||
| 62 | 11 | ▶ | } map { /::/ ? $_ : "$pkg$_" } @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 4 | ▶ | sub get_function_names_from_package { | ||||||||||||||||||
|
Subroutine
get_function_names_from_package: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 66 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 67 | 4 | ▶ | my $pkg = shift || caller; | ||||||||||||||||||
|
Condition33%1/3
shift() || caller
MC/DC0%0/2
shift() || caller
shift()
caller
Truth table33%1/3
A || B
A= shift()B= caller
| |||||||||||||||||||||
| 68 | 4 | my $symtab = *{ $pkg . "::" }{HASH}; | |||||||||||||||||||
| 69 | sort grep *$_{CODE}, # drop symbols w/o code | ||||||||||||||||||||
| 70 | 4 | map $pkg."::$_", # fully qualify | |||||||||||||||||||
| 71 | grep !/^_|^[_A-Z]+$/, # drop _underscored & ALL_CAPS | ||||||||||||||||||||
| 72 | keys %$symtab; # get all symbols for package | ||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 2 | my @init; | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 3 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 78 | 3 | shift; # don't need self | |||||||||||||||||||
| 79 | 3 | my $caller = caller; | |||||||||||||||||||
| 80 | 3 | ▶ | push @init, curry_package_c($caller) if grep /^:all$/, @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 3 | curry_named_functions_from_package($caller, grep !/^:/, @_); | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 2 | ▶ | INIT { finish_initialization() } | ||||||||||||||||||
|
Subroutine
INIT: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 2 | ▶ | sub finish_initialization { | ||||||||||||||||||
|
Subroutine
finish_initialization: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 87 | 2 | $_->() for @init; @init = (); | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | # physician, curry thyself! | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 2 | curry_named_functions(qw( | |||||||||||||||||||
| 93 | curry_package | ||||||||||||||||||||
| 94 | )); | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 2 | 1; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||