Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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 rifling through the Monastery: (5)
As of 2015-07-06 23:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (85 votes), past polls