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.

Replies are listed 'Best First'.
Re^2: Forward declaration of subs
by LanX (Saint) 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.