Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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 };

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://984170]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2018-02-20 14:22 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (271 votes). Check out past polls.