Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Forward declaration of subs

by BrowserUk (Pope)
on Oct 11, 2013 at 23:21 UTC ( #1057958=note: print w/ replies, xml ) Need Help??


in reply to Forward declaration of subs

Is there any practical application of forward declaration of subroutines?

Yes. If you have mutually recursive subroutines -- sub a calls sub b; and sub b calls sub a; -- then you need to forward declare one of them to pass strict, if you want to avoid using parens.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: Forward declaration of subs
Re^2: Forward declaration of subs
by LanX (Canon) on Oct 12, 2013 at 00:24 UTC
    > mutually recursive subroutines ... if you want to avoid using parens.

    As an illustration, the following doesn't work cause &b is unknown when &a is compiled.

    Without strict b will be converted to string "b" and returned. (strict prohibits this behaviour of barewords.)

    lanx@nc10-ubuntu:/tmp$ perl sub a { print "a$level "; b if $level-- }; sub b { print "b$level "; a if $level-- }; $level=5; print"\n"; a; print"\n"; b; __END__ a5 b4 a3

    with forward declaration

    lanx@nc10-ubuntu:/tmp$ perl sub b; sub a { print "a$level "; b if $level-- }; sub b { print "b$level "; a if $level-- }; $level=5; print"\n"; a; __END__ a5 b4 a3 b2 a1 b0

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Better addressed to the OP.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-10-20 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (92 votes), past polls