...
Code Block | ||||
---|---|---|---|---|
| ||||
sub print_array {
my $array = shift;
print "( ";
foreach $item (@{$array}) {
print "$item , ";
}
print ")\n";
}
my @array; # initialize
my $array_ref = @array;
print_array( $array_ref);
|
The developer mistakenly left out the \
indicator when initializing $array_ref
. Consequently, instead of a reference to the array, it contains the number of elements in the array. When passed to the print_array()
subroutine, this program prints an empty array.
...
Code Block | ||||
---|---|---|---|---|
| ||||
my $array_ref = \@array;
print_array( $array_ref);
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
my @array; # initialize
my $cardinality = @array;
print "The array has $cardinality elements\n";
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
my $cardinality = scalar( @array);
print "The array has $cardinality elements\n";
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
my $cardinality = $#array + 1;
print "The array has $cardinality elements\n";
|
...
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP05EXP06-PL | low | unlikely | medium | P2 | L3 |
Automated Detection
Tool | Diagnostic |
---|---|
B::Lint | context |
Bibliography
...
[ |
---|
...
...
...
, ProhibitBooleanGrep | |
[Conway 2005] | "Lists," p. 71 |
[Wall 2011] | perlfunc |
...
[ProhibitBooleanGrep|http://search.cpan.org/dist/Perl-Critic/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitBooleanGrep.pm] \[[Conway 2005|AA. Bibliography#Conway 2005]\] pg. 71 \[[Wall 2011|AA. Bibliography#Manpages]\] [perlfunc|http://perldoc.perl.org/perlfunc.html] 02. Expressions EXP30-PL. Do not use deprecated or obsolete functions