3

I am getting this error -> "Can't use 'defined(@array)' (Maybe you should just omit the defined()?)"

On this line of code:

if ( defined( @{ $linkbot{"$nroboton"} } ) ) {

How can i fix this?

  • 1
    See also [Can't use 'defined(@array)' warning in converting .obj to .h](https://stackoverflow.com/q/41980796/2173773) and [Replacing deprecated “defined(@array)” in a ternary operation](https://stackoverflow.com/q/48809624/2173773) – Håkon Hægland Oct 08 '18 at 18:43

2 Answers2

8

defined tests whether a scalar value is undef, therefore it's nonsensical on an array. You can test whether the scalar is defined before using it as an arrayref, or if you're trying to test if the array is empty, just removed the defined() as the error message says.

# if this hash value is defined
if (defined $linkbot{$nroboton}) {

# if this referenced array has elements
if (@{ $linkbot{$nroboton} }) {
Grinnz
  • 9,093
  • 11
  • 18
3

Use define on the variable $nroboton itself, and/or if (@{$linkbot{$nroboton}}) for the anonymous array whose reference is the value for that key, as explained.

Once you need to check on any of that it stands to reason that you may well also need to test whether there is a key $nroboton in the hash %linkbot, with exists

if ( exists $linkbot{$nroboton} ) { ... }   # warning if $nroboton undef

so altogether

if (defined $nroboton and exists $linkbot{$nroboton}) { ... }

and now you can check and work with the data in the arrayref, @{$linkbot{$nroboton}}.

Note that there is no need to double-quote that variable; it will be evaluated.

zdim
  • 64,580
  • 5
  • 52
  • 81