| 1 | package JavaScript::Code::Variable; | ||||||||||||||||||||||||||||
| 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[ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | JavaScript::Code::Element | ||||||||||||||||||||||||||||
| 7 | JavaScript::Code::Value | ||||||||||||||||||||||||||||
| 8 | JavaScript::Code::Expression::Node::Arithmetic | ||||||||||||||||||||||||||||
| 9 | 2 | ]; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use JavaScript::Code::Type (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use overload | ||||||||||||||||||||||||||||
| 15 | 0 | ▶ | '""' => sub { shift->full_name }, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 16 | 2 | 'eq' => \&same, | |||||||||||||||||||||||||||
| 17 | 2 | '==' => \&equal; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | __PACKAGE__->mk_accessors(qw[ index name declared ref ]); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | $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 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 2 | my $obj = shift; | |||||||||||||||||||||||||||
| 69 | 2 | ▶ | my $class = ref $obj || $obj; | ||||||||||||||||||||||||||
|
Condition33%1/3
CORE::ref $obj || $obj
MC/DC0%0/2
CORE::ref $obj || $obj
CORE::ref $obj
$obj
Truth table33%1/3
A || B
A= CORE::ref $objB= $obj
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||||||||||
| 72 | 2 | $self->value( $self->{value} ); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 2 | 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 | 4 | ▶ | sub full_name { | ||||||||||||||||||||||||||
|
Subroutine
full_name: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 4 | my $name = $self->name; | |||||||||||||||||||||||||||
| 93 | 4 | ▶ | die "No name defined." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | unless $name; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 4 | ▶ | die "Not a valid 'JavaScript::Code::Variable' name: '$name'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | unless $self->is_valid_name($name); | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 4 | ▶ | $name .= "[" . $self->index . "]" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | if defined $self->index; | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | return $name; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head2 $self->value( $value ) | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Gets or sets the value of the variable. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 8 | ▶ | sub value { | ||||||||||||||||||||||||||
|
Subroutine
value: called CC=7 SCAR=21.5
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 113 | 8 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 4 | my %args = (); | |||||||||||||||||||||||||||
| 116 | 4 | ▶ | if ( @_ == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 4 | ▶ | if ( ref $_[0] eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | %args = %{ $_[0] }; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||||||||||
| 121 | 4 | $args{value} = $_[0]; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||||||||||
| 125 | 0 | %args = @_; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | $self->{value} = | ||||||||||||||||||||||||||||
| 129 | 4 | JavaScript::Code::Type->build( value => delete $args{value} ); | |||||||||||||||||||||||||||
| 130 | 4 | ▶ | $self->{index} = $args{index} if exists $args{index}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 4 | return $self; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 4 | my $value = $self->{value}; | |||||||||||||||||||||||||||
| 137 | 4 | ▶ | $self->value($value) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
CORE::ref $value and $value->isa("JavaScript::Code::Value")
MC/DC0%0/2
CORE::ref $value and $value->isa("JavaScript::Code::Value")
CORE::ref $value
$value->isa("JavaScript::Code::Value")
Truth table33%1/3
A and B
A= CORE::ref $valueB= $value->isa("JavaScript::Code::Value")
| |||||||||||||||||||||||||||||
| 138 | unless ref $value | ||||||||||||||||||||||||||||
| 139 | and $value->isa('JavaScript::Code::Value'); | ||||||||||||||||||||||||||||
| 140 | 4 | 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 | 4 | ▶ | sub output { | ||||||||||||||||||||||||||
|
Subroutine
output: called CC=9 SCAR=23.5
Pod
documented
| |||||||||||||||||||||||||||||
| 161 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 162 | 4 | ▶ | my $scope = shift || 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 1
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 4 | my $name = $self->full_name; | |||||||||||||||||||||||||||
| 165 | 4 | ▶ | die "A 'JavaScript::Code::Variable' needs a name." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | unless $name; | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 4 | my $indenting = $self->get_indenting($scope); | |||||||||||||||||||||||||||
| 169 | 4 | my $output = ''; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 4 | ▶ | $self->declared(1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | if $name =~ m!\[.*\]$!; | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 4 | ▶ | my $declared = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | defined $self->declared | ||||||||||||||||||||||||||||
| 176 | ? $self->declared | ||||||||||||||||||||||||||||
| 177 | : $self->exists_in_parent($self); | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 4 | $output .= $indenting; | |||||||||||||||||||||||||||
| 180 | 4 | ▶ | $output .= 'var ' unless $declared; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 4 | $output .= $name; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 4 | ▶ | if ( defined( my $value = $self->value ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 4 | ▶ | die "Not a 'JavaScript::Code::Value'." | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
CORE::ref $value and $value->isa("JavaScript::Code::Value")
MC/DC0%0/2
CORE::ref $value and $value->isa("JavaScript::Code::Value")
CORE::ref $value
$value->isa("JavaScript::Code::Value")
Truth table33%1/3
A and B
A= CORE::ref $valueB= $value->isa("JavaScript::Code::Value")
| |||||||||||||||||||||||||||||
| 185 | unless ref $value | ||||||||||||||||||||||||||||
| 186 | and $value->isa('JavaScript::Code::Value'); | ||||||||||||||||||||||||||||
| 187 | 4 | $output .= ' = '; | |||||||||||||||||||||||||||
| 188 | 4 | $output .= $value->output; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 4 | $output .= ";\n"; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 4 | 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 | 2 | ▶ | sub same { | ||||||||||||||||||||||||||
|
Subroutine
same: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 203 | 2 | my ( $self, $other ) = @_; | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 2 | ▶ | return 0 unless $other->isa('JavaScript::Code::Variable'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 2 | 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 | 0 | ▶ | sub equal { | ||||||||||||||||||||||||||
|
Subroutine
equal: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 216 | 0 | my ( $self, $other ) = @_; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 0 | ▶ | return 0 unless $other->isa('JavaScript::Code::Variable'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 0 | return $self->value eq $other->value; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | L<JavaScript::Code> | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Sascha Kiefer, C<esskar@cpan.org> | ||||||||||||||||||||||||||||
| 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 | 2 | 1; | |||||||||||||||||||||||||||