Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Recursive method calls and references

by moritz (Cardinal)
on Jul 28, 2012 at 09:09 UTC ( #984170=note: print w/ replies, xml ) Need Help??


in reply to Recursive method calls and references

Adding parentheses around $$arg[0] fixes that alright, but I don't care.

But you should care. If adding parenthesis helps, it's a parsing problem. Here's what B::Deparse has to say to the code:

sub test_a { use warnings; use strict; my $arg = shift(); ref $arg ? $$arg->test_a([0]) : $arg; } sub test_b { use warnings; use strict; my $arg = shift(); ref $arg ? test_a($$arg[0]) : $arg; }

So the call to test_a inside test_a is parsed as indirect method call syntax. Why? Because it's not predeclared. In Perl 5, a name only becomes visible in the statement after the declaration, which is why you can't write

my $sub = sub { ...; $sub->(); .. };

So, in test_b you call test_a, which has already been declared. So either use parens after the function name, or predeclare it with

sub test_a; sub test_a { ...; recurse into test_a here };


Comment on Re: Recursive method calls and references
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://984170]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-12-28 09:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (180 votes), past polls