| 1 | package JavaScript::Code::Function; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use vars qw[ $VERSION ]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use base qw[ JavaScript::Code::Element ]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use JavaScript::Code::Function::Result (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use JavaScript::Code::Function::BuildIn (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | __PACKAGE__->mk_accessors(qw[ name parameters is_buildin ]); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | $VERSION = '0.08'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | JavaScript::Code::Function - A JavaScript Function | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 METHODS | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head2 $self->name( $name ) | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | Sets or gets the function name. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head2 $self->block( $block ) | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Sets or gets the code block of the function. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | I<$block> must be a L<JavaScript::Code::Block> | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub block { | ||||||||||||||||||||||||||
|
Subroutine
block: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | my $args = $self->args(@_); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | my $block = $args->{block} || shift; | ||||||||||||||||||||||||||
|
Condition0%0/3
$args->{'block'} || shift()
MC/DC0%0/2
$args->{'block'} || shift()
$args->{'block'}
shift()
Truth table0%0/3
A || B
A= $args->{'block'}B= shift()
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | die "Block must be a 'JavaScript::Code::Block'." | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $block and $block->isa("JavaScript::Code::Block")
MC/DC0%0/2
ref $block and $block->isa("JavaScript::Code::Block")
ref $block
$block->isa("JavaScript::Code::Block")
Truth table0%0/3
A and B
A= ref $blockB= $block->isa("JavaScript::Code::Block")
| |||||||||||||||||||||||||||||
| 42 | unless ref $block | ||||||||||||||||||||||||||||
| 43 | and $block->isa('JavaScript::Code::Block'); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | $self->{block} = $block->clone->parent($self); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | return $self; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||||||||||
| 50 | 0 | return $self->{block}; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head2 $self->is_buildin( ) | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Returns whether or not the function is a build-in function | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub is_buildin { return 0; } | ||||||||||||||||||||||||||
|
Subroutine
is_buildin: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head2 $self->check_name( ) | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub check_name { | ||||||||||||||||||||||||||
|
Subroutine
check_name: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | my $name = $self->name; | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | die "A 'JavaScript::Code::Function' needs a name." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | unless $name; | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | die "Not a valid 'JavaScript::Code::Function' name: '$name'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | unless $self->is_valid_name($name, $self->is_buildin ); | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | return $name; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head2 $self->call( ) | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Calls the functions. Returns a L<JavaScript::Code::Function::Result>. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | my $params = $self->args(@_)->{parameters} || $_[0] || []; | ||||||||||||||||||||||||||
|
Condition0%0/5
$self->args(@_)->{'parameters'} || $_[0] || []
$self->args(@_)->{"parameters"} or $_[0]
MC/DC0%0/2
$self->args(@_)->{'parameters'} || $_[0] || []
$self->args(@_)->{"parameters"}
$_[0]
Truth table0%0/3
A or B
A= $self->args(@_)->{"parameters"}B= $_[0]
| |||||||||||||||||||||||||||||
| 89 | 0 | my $name = $self->check_name; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $params = [$params] unless ref $params eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | my $result = ''; | |||||||||||||||||||||||||||
| 94 | 0 | $result .= "$name ( "; | |||||||||||||||||||||||||||
| 95 | 0 | $result .= join(", ", @{$params}); | |||||||||||||||||||||||||||
| 96 | 0 | $result .= " )"; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | return JavaScript::Code::Function::Result->new( value => $result ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head2 $self->output( ) | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub output { | ||||||||||||||||||||||||||
|
Subroutine
output: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | my $scope = shift || 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 1
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | die "Can not defined a build-in function." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | if $self->is_buildin; | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my $name = $self->check_name; | |||||||||||||||||||||||||||
| 113 | 0 | my $indenting = $self->get_indenting($scope); | |||||||||||||||||||||||||||
| 114 | 0 | my $output = ''; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | $output .= $indenting; | |||||||||||||||||||||||||||
| 117 | 0 | $output .= "function $name ( "; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | $self->parameters( [] ) unless defined $self->parameters; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | $output .= join( | ||||||||||||||||||||||||||||
| 122 | ', ', | ||||||||||||||||||||||||||||
| 123 | map { | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | die "Not a valid 'JavaScript::Code::Function' parameter name: '$_'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | unless $self->is_valid_name($_); | ||||||||||||||||||||||||||||
| 126 | 0 | $_ | |||||||||||||||||||||||||||
| 127 | 0 | } @{ $self->parameters } | |||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | $output .= " )\n"; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ( defined(my $block = $self->block) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | $output .= $block->output($scope); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | else { | ||||||||||||||||||||||||||||
| 136 | 0 | $output .= $indenting; | |||||||||||||||||||||||||||
| 137 | 0 | $output .= "{\n"; | |||||||||||||||||||||||||||
| 138 | 0 | $output .= $indenting; | |||||||||||||||||||||||||||
| 139 | 0 | $output .= "}\n"; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | return $output; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | L<JavaScript::Code> | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Sascha Kiefer, C<esskar@cpan.org> | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | This library is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 156 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 1; | ||||||||||||||||||||||||||||