Ornot
Ornot

Reputation: 63

CGI error Can't use an array as a reference

My configuration is Debian Stretch

I verify my Perl code with the command

line perl -wcT admin.cgi

I have an error in the code at this line:

print &select("$id-2",\@values,\@values,@{$FORMAT{$name}}->[1]),"<br /> \n";

The error is:

Can't use an array as a reference

I also tried with this web editor

It seems that the error is :

@{$FORMAT{$name}}->[1]

Upvotes: 1

Views: 1095

Answers (2)

Ornot
Ornot

Reputation: 63

my cgi called webadmin.cgi now is on ActivePerl with Xampp on my pc windows 7.

this one works (run perfectly) in local test with the same error:

print &select("$id-2",\@values,\@values,@{$FORMAT{$name}}->[1]),"<br /> \n";

and if install the same files on OS debian stretch (my dedicated server) finally:

if i test with the same error of syntax it doesn't work (error 500)

if i replace with twice solution, the program on line run but eject me.

thanks for your patience,

Ornot

Upvotes: 0

Dave Cross
Dave Cross

Reputation: 69314

As you say, the problem is here:

@{$FORMAT{$name}}->[1]

It appears that $FORMAT{$name} is expected to contain an array reference. And you want to get the second element from the referenced array. There are (at least!) two ways to do this.

You can dereference the array reference to get an array and then use standard array indexing brackets:

@{$FORMAT{$name}}[1]

Or you can use the deferencing arrow along with array indexing brackets:

$FORMAT{$name}->[1]

What you can't do (as you've found) is to use both syntaxes simultaneously :-)

Update: As Borodin points out in a comment, my first solution is incorrect. When accessing a single element from an array, you should change the @ to a $. So it should actually be:

${$FORMAT{$name}}[1]

And in my second solution, the arrow is actually optional (dereferencing arrows between two sets of brackets always are), so you can write:

$FORMAT{$name}[1]

Upvotes: 3

Related Questions