Code.pm

filter: Statementstmt 100% 41 / 41 Branchbran 62.5% 5 / 8 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 89.2% 58 / 65 CC 8 SCAR 12.5
CC
8
Cov
91%
output_for_html
6.2
output
3.0
BEGIN
1.0
CRAP 3.5
« Code/Element.pm ↑ Summary Code/Expression/Op/Term.pm »
1 package JavaScript::Code;
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::Block ];
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 2 ▶use JavaScript::Code::Condition ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JavaScript::Code::Variable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JavaScript::Code::Function ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 2 $VERSION = '0.08';
12
13 =head1 NAME
14
15 JavaScript::Code - A JavaScript Code Framework
16
17 =head1 SYNOPSIS
18
19 #!/usr/bin/perl
20
21 use strict;
22 use warnings;
23 use JavaScript::Code;
24 use JavaScript::Code::Condition;
25 use JavaScript::Code::Expression::Boolean qw/AND OR NOT EQUAL NOT_EQUAL/;
26 use JavaScript::Code::Variable;
27 use JavaScript::Code::Function;
28
29 # create the code object
30 my $code = JavaScript::Code->new();
31
32 # create a code block
33 my $block = JavaScript::Code::Block->new();
34
35 # create some variables
36 my $var1 = JavaScript::Code::Variable->new()->name('a');
37 my $var2 = JavaScript::Code::Variable->new()->name('b')->value(42);
38 my $var3 = JavaScript::Code::Variable->new()->name('c')->value(23);
39 my $var4 = JavaScript::Code::Variable->new()->name('x')->declared( 1 ); # could have been declared in a other script
40 my $var5 = JavaScript::Code::Variable->new()->name('y')->value( ['test', 'uhu', $var4] );
41
42 # add some of them to the code block
43 $block->add( [ $var2, $var3] );
44
45 # create some numbers
46 my $x1 = JavaScript::Code::Number->new()->value( 10 );
47 my $x2 = JavaScript::Code::Number->new()->value( 20 );
48 my $x3 = JavaScript::Code::Number->new()->value( 30 );
49
50 # build expressions
51 my $c0 = $x1 + $x2;
52 my $c1 = $var2 - ($c0 + $x1->value(30)) * $x2->value(40);
53 my $c2 = $c1 * $var3;
54
55 # assign a expression to a variable and add it to your code block
56 $block->add( JavaScript::Code::Variable->new()->name('d')->value( $c2 ) );
57
58 # add more stuff to your code block
59 $block->add( JavaScript::Code::Block->new()->add( $var1->value("Bar!") ) );
60 $block->add( $var1->clone->value("Foo!") ); # clone 'a' and give it a new value
61
62 # boolean expressions
63 my $foo = $c0->as_variable( name => 'foo' );
64 my $e1 = EQUAL( $foo, $x3 );
65
66 # conditions
67 my $cond = JavaScript::Code::Condition->new;
68 $cond->add_if( expression => $e1, block => $block );
69 $cond->add_if( expression => NOT_EQUAL( $foo, $x3 ), block => $block );
70 $cond->else( $block );
71
72 # add your block and other stuff to the code object
73 $code->add( $foo );
74 $code->add( [ $var4->value( 4711 ), $var1->value( "Perl!" ) ] );
75 $code->add( $cond );
76 $code->add( $var2->value(21) ); # 'b' with value 21 (note the different scope)
77 $code->add( $var5 );
78 $code->add_variable( name => "y", value => 'Larry', index => 16 );
79
80 #functions
81 my $func1block = JavaScript::Code::Block->new();
82 $func1block->add( JavaScript::Code::Function::BuildIn->return->call( '"test"')->as_element );
83
84 my $func1 = JavaScript::Code::Function->new( name => "foofunc", block => $func1block );
85 $func1->parameters( qw[ p1 p2 p3 p4 ] );
86 $code->add( $func1 );
87 $code->add( $func1->call([ $var5, $var2, '"foo"', 12 ])->as_variable( name => 'result' ) );
88
89 # output to be embedded in your html code
90 print $code->output_for_html;
91
92
93 =head1 DESCRIPTION
94
95 Create javascript-code!
96
97 =head1 METHODS
98
99 =head2 $self->add( $element )
100
101 Adds a new element.
102
103 =cut
104
105 =head2 $self->elements( )
106
107 Returns a ref-array of all added elements.
108
109 =cut
110
111 =head2 $self->output( )
112
113 Returns the javascript-code.
114
115 =cut
116
117 1 ▶sub output {
Subroutine output: called CC=3 SCAR=11.0
Pod documented
118 1 my $self = shift;
119 1 ▶ my $intend = shift || 0;
Condition50%1/2
shift() || 0
l!l
01
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
120
121 1 my $output = '';
122
123 1 my $elements = $self->elements;
124 1 foreach my $element ( @{$elements} ) {
125 2 $output .= $element->output;
126 }
127
128 1 return $output;
129 }
130
131 =head2 $self->output_for_html( < \%args > )
132
133 Returns the javascript-code that can be directly embedded into html-code.
134
135 Optimal \%args: Key-value pairs that reprensent attributes that will be associated to the script tag.
136
137 print $code->output_for_html( { language => undef } );
138
139 =cut
140
141 1 ▶sub output_for_html {
Subroutine output_for_html: called CC=6 SCAR=18.2
Pod documented
142 1 my ( $self, $args ) = @_;
143
144 1 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
01
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
145 $args->{language} =
146 exists $args->{language}
147 ? $args->{language}
148 1 ▶ : 'Javascript';
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'language'} ? : 0 1 1
149 $args->{type} =
150 exists $args->{type}
151 ? $args->{type}
152 1 ▶ : 'text/javascript';
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'type'} ? : 0 1 1
153
154 1 my $output = '<script';
155 1 ▶ while ( my ( $key, $val ) = each %{$args} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$args;}) 2 1 3
156 2 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 2 2
157 2 $output .= qq~ $key="$val"~;
158 }
159 1 $output .= "><!--\n\n";
160 1 $output .= $self->output;
161 1 $output .= "\n// --></script>\n";
162
163 1 return $output;
164 }
165
166 =head1 SEE ALSO
167
168 L<JavaScript::MochiKit>
169
170 L<http://www.perl-community.de>
171
172 =head1 AUTHOR
173
174 Sascha Kiefer, C<esskar@cpan.org>
175
176 =head1 LICENSE
177
178 This library is free software, you can redistribute it and/or modify it under
179 the same terms as Perl itself.
180
181 =cut
182
183 2 1;
« Code/Element.pm ↑ Summary Code/Expression/Op/Term.pm »