http://www.perlmonks.org?node_id=984162

Grimy has asked for the wisdom of the Perl Monks concerning the following question:

Howdy, monks. I hope you can help me understand this doozy:
use strict; use warnings; sub test_a { my $arg = shift; ref($arg) ? test_a$$arg[0] : $arg } sub test_b { my $arg = shift; ref($arg) ? test_a$$arg[0] : $arg } print test_b[42]; # prints '42' print test_a[42]; # dies 'Not a SCALAR reference at test line 3
Okay, so those two subs are exactly identical, yet one runs just fine and the other crashes. My only guess is that the recursive nature of test_a somewhat has an impact on the context given to the reference. Adding parentheses around $$arg[0] fixes that alright, but I don't care. I just want to understand.