in reply to IF statements not being evaluated as expected

You're testing if the array is equal to 1 when you should be testing the first element of the array.

What does it mean for an array to be equal to one? It means it contains one element. An array returns the number of elements it containes when evaluated in scalar context.

You could use $_[0] directly, but it's traditional to name your arguments. Fixed:

sub test { my ($arg) = @_; print "$arg\n"; if ($arg == 1) { print "entering loop 1\n"; $hashname = "hash1"; } elsif ($arg == 2) { print "entering loop 2\n"; $hashname = "hash2"; } print "$hashname\n"; return ($hashname); }

Simplified:

sub test { my ($arg) = @_; if ($arg == 1) { return 'hash1'; } elsif ($arg == 2) { return 'hash2'; } die; }