Voglio ripetere un riferimento a un array di hash senza dover fare copie locali, ma continuo a ottenere Can not use string ("1") come ARRAY ref mentre "strict refs "errori. Perché? Come lo aggiusto?Riferimenti in Perl: Array of Hashes
sub hasGoodCar {
my @garage = (
{
model => "BMW",
year => 1999
},
{
model => "Mercedes",
year => 2000
},
);
run testDriveCars(\@garage);
}
sub testDriveCars {
my $garage = @_;
foreach my $car (@{$garage}) { # <=========== Can't use string ("1") as an ARRAY ref while "strict refs" error
return 1 if $car->{model} eq "BMW";
}
return 0;
}
Questo errore comune deriva dal non comprendere il contesto. L'assegnazione ad uno scalare produce un contesto scalare, quindi '@ _' viene valutato come uno scalare (che produce la lunghezza). – daotoad