Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Forward declaration of subs

by perlaintdead (Scribe)
on Oct 11, 2013 at 23:16 UTC ( #1057957=perlquestion: print w/ replies, xml ) Need Help??
perlaintdead has asked for the wisdom of the Perl Monks concerning the following question:

Is there any practical application of forward declaration of subroutines?

sub NAME;

Comment on Forward declaration of subs
Download Code
Re: Forward declaration of subs
by BrowserUk (Pope) on Oct 11, 2013 at 23:21 UTC
    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.
      > 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.
Re: Forward declaration of subs
by 2teez (Priest) on Oct 11, 2013 at 23:30 UTC

    Hi perlaintdead,
    With forward declaration of subroutine, you can omit the parentheses usage like thus:

    use warnings; use strict; sub printer; printer 'hello','world','again'; # NOTE HERE sub printer{ print $_,$/ for @_; }
    Without the forward declaration one would have to do
    ... printer(...); ...
    #Update: ++BrowserUk faster fingers!!!

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Forward declaration of subs
by chromatic (Archbishop) on Oct 11, 2013 at 23:34 UTC

    If you generate functions as needed with AUTOLOAD(), forward declarations make can() work correctly without you having to override it for your package.

Re: Forward declaration of subs
by perlaintdead (Scribe) on Oct 12, 2013 at 05:13 UTC

    thanks

Re: Forward declaration of subs
by ikegami (Pope) on Oct 14, 2013 at 04:46 UTC
    >perl -wE"sub foo { bar('a','b'); } sub bar($) { } foo(); say 'done'" done >perl -wE"sub bar($); sub foo { bar('a','b'); } sub bar($) { } foo(); +say 'done'" Too many arguments for main::bar at -e line 1, near "'b')" Execution of -e aborted due to compilation errors.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-12-26 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls