foreach my $elem (@$arrayRef) { foreach () { foreach () { } } # commenting out this below loop avoids the seg fault foreach () { } }