| 1 | |||||||||||
| 2 | # See the POD documentation at the end of this | ||||||||||
| 3 | # document for detailed copyright information. | ||||||||||
| 4 | # (c) 2002-2006 Steffen Mueller, all rights reserved. | ||||||||||
| 5 | |||||||||||
| 6 | package Math::Project3D::Function; | ||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use vars qw/$VERSION/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | $VERSION = 1.02; | |||||||||
| 13 | |||||||||||
| 14 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | # Uncreatively named public class method "new" | ||||||||||
| 18 | # | ||||||||||
| 19 | # One of three syntaxes may be used: | ||||||||||
| 20 | # new(coderefs) | ||||||||||
| 21 | # new(param_names, expressions) | ||||||||||
| 22 | # new(param_names, expressions_and_coderefs_mixed) | ||||||||||
| 23 | # Returns a compiled anonymous subroutine | ||||||||||
| 24 | |||||||||||
| 25 | 6 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 26 | 6 | my @components = @_; | |||||||||
| 27 | |||||||||||
| 28 | # 'tis a class method after all, so remove the | ||||||||||
| 29 | # class name if necessary. | ||||||||||
| 30 | 6 | ▶ | shift @components if $components[0] eq __PACKAGE__; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | |||||||||||
| 32 | # Check for the existance of plaintext components | ||||||||||
| 33 | 6 | my $has_uncompiled_components = 0; | |||||||||
| 34 | 6 | foreach my $component (@components) { | |||||||||
| 35 | 8 | ▶ | $has_uncompiled_components++, last | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | if ref $component ne 'CODE'; | ||||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 6 | my $function; | |||||||||
| 40 | |||||||||||
| 41 | # Call another subroutine to do the dirty work of | ||||||||||
| 42 | # compiling plaintext expressions if necessary. | ||||||||||
| 43 | 6 | ▶ | if ($has_uncompiled_components) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 5 | my $param_names = shift @components; | |||||||||
| 45 | 5 | $function = _from_uncompiled_components($param_names, \@components); | |||||||||
| 46 | } else { | ||||||||||
| 47 | |||||||||||
| 48 | # Have the function return whatever the applied components | ||||||||||
| 49 | # return. @components is accessible because | ||||||||||
| 50 | # the function in $function is a closure. | ||||||||||
| 51 | 25 | ▶ | $function = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 25 | return map { $_->(@_) } @components; | |||||||||
| 53 | 1 | }; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 6 | return $function; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | |||||||||||
| 60 | # internal subroutine _from_uncompiled_components | ||||||||||
| 61 | # | ||||||||||
| 62 | # Does the dirty work of compiling plaintext expressions. | ||||||||||
| 63 | # Ugly. Very Ugly. | ||||||||||
| 64 | # Takes a string of parameter names (without dollar) | ||||||||||
| 65 | # separated by commas and an array reference to an | ||||||||||
| 66 | # array of components as arguments. | ||||||||||
| 67 | # Returns compiled function (anon sub). | ||||||||||
| 68 | |||||||||||
| 69 | 5 | ▶ | sub _from_uncompiled_components { | ||||||||
|
Subroutine
_from_uncompiled_components: called CC=4 SCAR=13.9
| |||||||||||
| 70 | # parameter names separated by commas | ||||||||||
| 71 | 5 | my @param_names = split /,/, shift; | |||||||||
| 72 | 5 | my $components = shift; | |||||||||
| 73 | |||||||||||
| 74 | # This var will hold the plaintext of the function we will compile | ||||||||||
| 75 | # later. | ||||||||||
| 76 | 5 | my $function_string = "sub {\n"; | |||||||||
| 77 | |||||||||||
| 78 | # For all declared parameter names, alias the n-th function | ||||||||||
| 79 | # argument to a lexical of the n-th name. | ||||||||||
| 80 | 5 | ▶ | for (my $param_no = 0; $param_no < @param_names; $param_no++ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 16 | $function_string .= " my \$$param_names[$param_no] = \$_[$param_no];\n"; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | # Return results of the applied component functions or expressions. | ||||||||||
| 85 | 5 | $function_string .= " return "; | |||||||||
| 86 | |||||||||||
| 87 | 5 | my $component_count = 0; | |||||||||
| 88 | |||||||||||
| 89 | 5 | foreach my $component (@$components) { | |||||||||
| 90 | |||||||||||
| 91 | 15 | ▶ | if (ref $component eq 'CODE') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | |||||||||||
| 93 | # We're a coderef. Run the anon sub associated with this | ||||||||||
| 94 | # component number and supply it with all function args. | ||||||||||
| 95 | 1 | $function_string .= "\$components[$component_count]->(\@_), "; | |||||||||
| 96 | |||||||||||
| 97 | } else { | ||||||||||
| 98 | |||||||||||
| 99 | # We're a plaintext expression. Insert it, but wrap a | ||||||||||
| 100 | # "scalar()" around it to prevent expressions like "x,y" | ||||||||||
| 101 | # from screwing up the order of return values. | ||||||||||
| 102 | 14 | $function_string .= "scalar($component), "; | |||||||||
| 103 | |||||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 15 | $component_count++; | |||||||||
| 107 | |||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 5 | $function_string .= "\n};"; | |||||||||
| 111 | |||||||||||
| 112 | # Call yet another subroutine to make sure the number | ||||||||||
| 113 | # of lexicals in scope is minimal. | ||||||||||
| 114 | 5 | return _compile_function($components, $function_string); | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | |||||||||||
| 118 | # Evil evaluator subroutine _compile_function | ||||||||||
| 119 | # | ||||||||||
| 120 | # Takes an array ref of components and a function_string | ||||||||||
| 121 | # as arguments. | ||||||||||
| 122 | # Returns compiled function or croaks about evaluation errors. | ||||||||||
| 123 | |||||||||||
| 124 | 5 | ▶ | sub _compile_function { | ||||||||
|
Subroutine
_compile_function: called CC=2 SCAR=7.0
| |||||||||||
| 125 | |||||||||||
| 126 | # We want an array for speed. Dereferencing every time is | ||||||||||
| 127 | # quite a bit slow IIRC. | ||||||||||
| 128 | # This array will be accessed by the function (closure) in | ||||||||||
| 129 | # order to call the precompiled component functions. | ||||||||||
| 130 | 5 | my @components = @{+shift(@_)}; | |||||||||
| 131 | |||||||||||
| 132 | # Do it. Mwaha. | ||||||||||
| 133 | 5 | my $function = eval shift; | |||||||||
| 134 | 5 | ▶ | croak $@ if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | |||||||||||
| 136 | # All went well. | ||||||||||
| 137 | 5 | return $function; | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | |||||||||||
| 141 | |||||||||||
| 142 | 2 | 1; | |||||||||
| 143 | |||||||||||
| 144 | __END__ | ||||||||||