line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JavaScript::Code::Variable;
|
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
11
|
use strict;
|
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
80
|
|
4
|
2
|
|
|
2
|
|
10
|
use vars qw[ $VERSION ];
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
95
|
|
5
|
2
|
|
|
|
|
1350
|
use base qw[
|
6
|
|
|
|
|
|
|
JavaScript::Code::Element
|
7
|
|
|
|
|
|
|
JavaScript::Code::Value
|
8
|
|
|
|
|
|
|
JavaScript::Code::Expression::Node::Arithmetic
|
9
|
2
|
|
|
2
|
|
10
|
];
|
|
2
|
|
|
|
|
4
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
9
|
use Scalar::Util ();
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
25
|
|
12
|
2
|
|
|
2
|
|
1068
|
use JavaScript::Code::Type ();
|
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
100
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use overload
|
15
|
0
|
|
|
0
|
|
0
|
'""' => sub { shift->full_name },
|
16
|
2
|
|
|
|
|
21
|
'eq' => \&same,
|
17
|
2
|
|
|
2
|
|
12
|
'==' => \&equal;
|
|
2
|
|
|
|
|
3
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw[ index name declared ref ]);
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
$VERSION = '0.08';
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 NAME
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
JavaScript::Code::Variable - A JavaScript Variable Element
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
#!/usr/bin/perl
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
use strict;
|
32
|
|
|
|
|
|
|
use warnings;
|
33
|
|
|
|
|
|
|
use JavaScript::Code::Variable;
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $string = JavaScript::Code::Variable->new();
|
36
|
|
|
|
|
|
|
$string->name( 'a' );
|
37
|
|
|
|
|
|
|
$string->value( 'This is a Test!' );
|
38
|
|
|
|
|
|
|
print $string->output;
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $number = JavaScript::Code::Variable->new()->name('b')->value( 288957 );
|
41
|
|
|
|
|
|
|
print $number->output;
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $strnumber = JavaScript::Code::Variable->new( name => 'c' );
|
44
|
|
|
|
|
|
|
$strnumber->value( JavaScript::Code::String->new(value => 288957) );
|
45
|
|
|
|
|
|
|
print $strnumber->output;
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $array = JavaScript::Code::Variable->new( { name => 'd' } );
|
48
|
|
|
|
|
|
|
$array->value( [0, 1] );
|
49
|
|
|
|
|
|
|
print $array->output;
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
A JavaScript Variable Element Class
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Example:
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
var a = 42; // Variable a with a nummeric value of 42
|
59
|
|
|
|
|
|
|
var b = "Test!"; // Variable b with a string value of "Test!"
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 METHODS
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head2 new
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=cut
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub new {
|
68
|
2
|
|
|
2
|
1
|
12
|
my $obj = shift;
|
69
|
2
|
|
33
|
|
|
10
|
my $class = ref $obj || $obj;
|
70
|
|
|
|
|
|
|
|
71
|
2
|
|
|
|
|
15
|
my $self = $class->SUPER::new(@_);
|
72
|
2
|
|
|
|
|
666
|
$self->value( $self->{value} );
|
73
|
|
|
|
|
|
|
|
74
|
2
|
|
|
|
|
13
|
return $self;
|
75
|
|
|
|
|
|
|
}
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 $self->name( $name )
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Gets or sets the name of the variable.
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=cut
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head2 $self->full_name( )
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Gets the full name, that is, the name including the any index information
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub full_name {
|
90
|
4
|
|
|
4
|
1
|
6
|
my $self = shift;
|
91
|
|
|
|
|
|
|
|
92
|
4
|
|
|
|
|
10
|
my $name = $self->name;
|
93
|
4
|
50
|
|
|
|
26
|
die "No name defined."
|
94
|
|
|
|
|
|
|
unless $name;
|
95
|
|
|
|
|
|
|
|
96
|
4
|
50
|
|
|
|
18
|
die "Not a valid 'JavaScript::Code::Variable' name: '$name'"
|
97
|
|
|
|
|
|
|
unless $self->is_valid_name($name);
|
98
|
|
|
|
|
|
|
|
99
|
4
|
50
|
|
|
|
22
|
$name .= "[" . $self->index . "]"
|
100
|
|
|
|
|
|
|
if defined $self->index;
|
101
|
|
|
|
|
|
|
|
102
|
4
|
|
|
|
|
25
|
return $name;
|
103
|
|
|
|
|
|
|
}
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 $self->value( $value )
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Gets or sets the value of the variable.
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub value {
|
112
|
8
|
|
|
8
|
1
|
26
|
my $self = shift;
|
113
|
8
|
100
|
|
|
|
25
|
if (@_) {
|
114
|
|
|
|
|
|
|
|
115
|
4
|
|
|
|
|
9
|
my %args = ();
|
116
|
4
|
50
|
|
|
|
31
|
if ( @_ == 1 ) {
|
117
|
4
|
50
|
|
|
|
14
|
if ( ref $_[0] eq 'HASH' ) {
|
118
|
0
|
|
|
|
|
0
|
%args = %{ $_[0] };
|
|
0
|
|
|
|
|
0
|
|
119
|
|
|
|
|
|
|
}
|
120
|
|
|
|
|
|
|
else {
|
121
|
4
|
|
|
|
|
11
|
$args{value} = $_[0];
|
122
|
|
|
|
|
|
|
}
|
123
|
|
|
|
|
|
|
}
|
124
|
|
|
|
|
|
|
else {
|
125
|
0
|
|
|
|
|
0
|
%args = @_;
|
126
|
|
|
|
|
|
|
}
|
127
|
|
|
|
|
|
|
|
128
|
4
|
|
|
|
|
25
|
$self->{value} =
|
129
|
|
|
|
|
|
|
JavaScript::Code::Type->build( value => delete $args{value} );
|
130
|
4
|
50
|
|
|
|
11
|
$self->{index} = $args{index} if exists $args{index};
|
131
|
|
|
|
|
|
|
|
132
|
4
|
|
|
|
|
12
|
return $self;
|
133
|
|
|
|
|
|
|
}
|
134
|
|
|
|
|
|
|
else {
|
135
|
|
|
|
|
|
|
|
136
|
4
|
|
|
|
|
6
|
my $value = $self->{value};
|
137
|
4
|
50
|
33
|
|
|
46
|
$self->value($value)
|
138
|
|
|
|
|
|
|
unless ref $value
|
139
|
|
|
|
|
|
|
and $value->isa('JavaScript::Code::Value');
|
140
|
4
|
|
|
|
|
11
|
return $self->{value};
|
141
|
|
|
|
|
|
|
}
|
142
|
|
|
|
|
|
|
}
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head2 $self->declared( undef | 0 | 1 )
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
If set to undef, the module will check the scope to decide whether or not the variable should be declared.
|
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
If set to 0, the module is undeclared und will be declared in its context.
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
If set to 1, the variable will to be declared in its context.
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=cut
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 $self->output( )
|
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Returns the javascript-code for that variable.
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub output {
|
161
|
4
|
|
|
4
|
1
|
15
|
my $self = shift;
|
162
|
4
|
|
50
|
|
|
16
|
my $scope = shift || 1;
|
163
|
|
|
|
|
|
|
|
164
|
4
|
|
|
|
|
11
|
my $name = $self->full_name;
|
165
|
4
|
50
|
|
|
|
8
|
die "A 'JavaScript::Code::Variable' needs a name."
|
166
|
|
|
|
|
|
|
unless $name;
|
167
|
|
|
|
|
|
|
|
168
|
4
|
|
|
|
|
19
|
my $indenting = $self->get_indenting($scope);
|
169
|
4
|
|
|
|
|
6
|
my $output = '';
|
170
|
|
|
|
|
|
|
|
171
|
4
|
50
|
|
|
|
10
|
$self->declared(1)
|
172
|
|
|
|
|
|
|
if $name =~ m!\[.*\]$!;
|
173
|
|
|
|
|
|
|
|
174
|
4
|
50
|
|
|
|
13
|
my $declared =
|
175
|
|
|
|
|
|
|
defined $self->declared
|
176
|
|
|
|
|
|
|
? $self->declared
|
177
|
|
|
|
|
|
|
: $self->exists_in_parent($self);
|
178
|
|
|
|
|
|
|
|
179
|
4
|
|
|
|
|
7
|
$output .= $indenting;
|
180
|
4
|
50
|
|
|
|
10
|
$output .= 'var ' unless $declared;
|
181
|
4
|
|
|
|
|
5
|
$output .= $name;
|
182
|
|
|
|
|
|
|
|
183
|
4
|
50
|
|
|
|
9
|
if ( defined( my $value = $self->value ) ) {
|
184
|
4
|
50
|
33
|
|
|
30
|
die "Not a 'JavaScript::Code::Value'."
|
185
|
|
|
|
|
|
|
unless ref $value
|
186
|
|
|
|
|
|
|
and $value->isa('JavaScript::Code::Value');
|
187
|
4
|
|
|
|
|
5
|
$output .= ' = ';
|
188
|
4
|
|
|
|
|
14
|
$output .= $value->output;
|
189
|
|
|
|
|
|
|
}
|
190
|
|
|
|
|
|
|
|
191
|
4
|
|
|
|
|
8
|
$output .= ";\n";
|
192
|
|
|
|
|
|
|
|
193
|
4
|
|
|
|
|
19
|
return $output;
|
194
|
|
|
|
|
|
|
}
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head2 $self->same( $var )
|
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Two variables are the same if they have the same name.
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=cut
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub same {
|
203
|
2
|
|
|
2
|
1
|
3
|
my ( $self, $other ) = @_;
|
204
|
|
|
|
|
|
|
|
205
|
2
|
50
|
|
|
|
8
|
return 0 unless $other->isa('JavaScript::Code::Variable');
|
206
|
2
|
|
|
|
|
5
|
return $self->name eq $other->name;
|
207
|
|
|
|
|
|
|
}
|
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head2 $self->equal( $var )
|
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Two variables are equal if they have the same value.
|
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=cut
|
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub equal {
|
216
|
0
|
|
|
0
|
1
|
|
my ( $self, $other ) = @_;
|
217
|
|
|
|
|
|
|
|
218
|
0
|
0
|
|
|
|
|
return 0 unless $other->isa('JavaScript::Code::Variable');
|
219
|
0
|
|
|
|
|
|
return $self->value eq $other->value;
|
220
|
|
|
|
|
|
|
}
|
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=head1 SEE ALSO
|
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
L
|
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head1 AUTHOR
|
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
Sascha Kiefer, C
|
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head1 LICENSE
|
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
This library is free software, you can redistribute it and/or modify it under
|
233
|
|
|
|
|
|
|
the same terms as Perl itself.
|
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=cut
|
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
1;
|