use overload; use Carp; sub flatten { my $ref = shift; my $seen = shift || ""; return map { (UNIVERSAL::isa($_,"ARRAY")) ? do { my $ref_str=overload::StrVal($_); Carp::croak "Cant flatten cyclic data structure !" if index($seen,$ref_str)>=0; flatten($_,$seen."$ref_str"); } : $_ #change to #} : $_ ? $_ :()# to simulate grep } @$ref; } require Data::Dumper; print Data::Dumper::Dumper([ flatten([qw(array of elements), [qw(and arrays)], bless [qw(even blessed ones)],"FooArray" ]) ]); __END__ $VAR1 = [ 'array', 'of', 'elements', 'and', 'arrays', 'even', 'blessed', 'ones' ];