All of this is described in perldoc perlfunc in the ref section.
ref returns false if its parameter is not a reference. It will return SCALAR only if the parameter is a reference to a scalar.
You may also need to know that to refer to blissful data - the Perl object - ref returns the class in which the data was included, and not the underlying data type. If you need to make a distinction between them, then the Scalar::Util module provides blessed , which returns a class that has blessed data and a reftype that returns the type of the underlying data, just like ref
You can do foobar recursion to handle an undefined nested data structure like
use strict; use warnings 'all'; use feature 'say'; my @array = ( "string1", [ "ele1_arraystr1", "ele1_arraystr2" ], "string2", [ "ele4_arraystr1", "ele4_arraystr2" ], "etc", [ "etc1", "etc2" ] ); foobar(\@array); sub foobar { my ($val, $indent) = (@_); $indent //= 0; my $ref = ref $val; if ( $ref eq 'ARRAY' ) { foobar($_, $indent+1) for @$val; } elsif ( not $ref ) { say ' ' x $indent, $val; } }
Exit
string1 ele1_arraystr1 ele1_arraystr2 string2 ele4_arraystr1 ele4_arraystr2 etc etc1 etc2
Alternatively, if your array always consists of alternating strings and array references, it might be easier for you to simply assign its hash. This would use strings as hash keys with their corresponding array references as hash values.
This code shows the idea. I used Data::Dump to show the resulting data structure
my %data = @array; use Data::Dump; dd \%data;
Exit
{ etc => ["etc1", "etc2"], string1 => ["ele1_arraystr1", "ele1_arraystr2"], string2 => ["ele4_arraystr1", "ele4_arraystr2"], }