line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JavaScript::Code;
|
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
58558
|
use strict;
|
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
84
|
|
4
|
2
|
|
|
2
|
|
8
|
use vars qw[ $VERSION ];
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
104
|
|
5
|
2
|
|
|
2
|
|
9
|
use base qw[ JavaScript::Code::Block ];
|
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
1250
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
1055
|
use JavaScript::Code::Condition ();
|
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
34
|
|
8
|
2
|
|
|
2
|
|
12
|
use JavaScript::Code::Variable ();
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
26
|
|
9
|
2
|
|
|
2
|
|
9
|
use JavaScript::Code::Function ();
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
524
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
sub output {
|
118
|
1
|
|
|
1
|
1
|
2
|
my $self = shift;
|
119
|
1
|
|
50
|
|
|
7
|
my $intend = shift || 0;
|
120
|
|
|
|
|
|
|
|
121
|
1
|
|
|
|
|
1
|
my $output = '';
|
122
|
|
|
|
|
|
|
|
123
|
1
|
|
|
|
|
12
|
my $elements = $self->elements;
|
124
|
1
|
|
|
|
|
5
|
foreach my $element ( @{$elements} ) {
|
|
1
|
|
|
|
|
3
|
|
125
|
2
|
|
|
|
|
6
|
$output .= $element->output;
|
126
|
|
|
|
|
|
|
}
|
127
|
|
|
|
|
|
|
|
128
|
1
|
|
|
|
|
4
|
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
|
|
|
|
|
|
|
sub output_for_html {
|
142
|
1
|
|
|
1
|
1
|
2
|
my ( $self, $args ) = @_;
|
143
|
|
|
|
|
|
|
|
144
|
1
|
|
50
|
|
|
7
|
$args ||= {};
|
145
|
1
|
50
|
|
|
|
5
|
$args->{language} =
|
146
|
|
|
|
|
|
|
exists $args->{language}
|
147
|
|
|
|
|
|
|
? $args->{language}
|
148
|
|
|
|
|
|
|
: 'Javascript';
|
149
|
1
|
50
|
|
|
|
6
|
$args->{type} =
|
150
|
|
|
|
|
|
|
exists $args->{type}
|
151
|
|
|
|
|
|
|
? $args->{type}
|
152
|
|
|
|
|
|
|
: 'text/javascript';
|
153
|
|
|
|
|
|
|
|
154
|
1
|
|
|
|
|
2
|
my $output = '\n";
|
162
|
|
|
|
|
|
|
|
163
|
1
|
|
|
|
|
6
|
return $output;
|
164
|
|
|
|
|
|
|
}
|
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 SEE ALSO
|
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
L
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
L
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 AUTHOR
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Sascha Kiefer, C
|
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
|
|
|
|
|
|
|
1;
|