perl: Can't use string as an ARRAY ref while "strict refs" in use
arrays, perl
Solution
You're declaring your arrays wrongly which is why the Dumper output has `[]` (an empty array reference) as the first element in `@players`. Use:
my @players = ();
my @playerscores = ();
The second error comes from:
my @testee = @$_[0];
This attempts to dereference `$_` and take the first element from the resulting array. You mean:
my @testee = @{$_[0]};
Which takes the first element from `@_` and dereferences it.
Problem
I have this problem where I'm trying to pass an array to a sub and it reads some other value. In this test script I'm passing a reference of array @players but its reading string $buffer instead. ``` use Data::Dumper; my @players = []; my @playerscores = []; my %FORM; my $buffer = "numplayers=2&changeplayers=3&CHANGEIT=CHANGEIT&player1=a&player2=b&restart=1&newcoords="; sub testsub { my @testee = @$_[0]; print "in testsub: $testee[0]\n"; } my @holder = split(/&/,$buffer,); foreach my $iter (@holder) { my ($name,$value) = split(/=/,$iter); $FORM{$name} = $value; } $_ = $buffer; foreach my $key (keys(%FORM)) { if($key=~ /player[1-9]/) { if(!($FORM{$key} eq "")) { my $holder = $key; $holder =~ s/player//; $players[$holder] = $FORM{$key}; $playerscores[$holder] = 0; $_ = $buffer; } } } print "\n Data Dumper on player:\n"; print Dumper(@players); print "\n\n"; print "Check sub:\n"; testsub(\@players,\@playerscores); ``` output: ``` Data Dumper on player: $VAR1 = []; $VAR2 = 'a'; $VAR3 = 'b'; Check sub: Can't use string ("numplayers=2 &changeplayers=3&CHA"...) as an ARRAY ref while "strict refs" in use at test-str-pool2.pl line 15. ``` Whereas I was expecting "a" or "b" as the result of `print "in testsub: $testee[0]\n";`. Why is this happening?