Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2016-02-10 06:10 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (332 votes), past polls