Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Defining a sub within a sub: OK?

by Joost (Canon)
on Oct 14, 2009 at 22:52 UTC ( #801235=note: print w/ replies, xml ) Need Help??


in reply to Re: Defining a sub within a sub: OK?
in thread Defining a sub within a sub: OK?

There's no sane way in which perl could handle a call to b() from the outside.

However there's nothing wrong with defining an anonymous subroutine inside another sub. Actually it's quite common, and used very often in functional programming.

Those two points are related: logically, any call to a() would redefine b() in that code, like my example below, which would only be silly. Also, that would mean that you couldn't call b() before calling a() at least once. But named subroutines are defined only once, at compile time*, which makes the whole construct iffy.

sub a { my $x = 3; *b = sub { print $x, $/; }; }
* The reasoning behind sub NAME { } constructs being realized early is so that you can call subroutines even if their definition is "later" in the code.


Comment on Re^2: Defining a sub within a sub: OK?
Download Code
Re^3: Defining a sub within a sub: OK?
by moritz (Cardinal) on Oct 14, 2009 at 23:06 UTC
    * The reasoning behind sub NAME { } constructs being realized early is so that you can call subroutines even if their definition is "later" in the code.

    That's mostly true, but not very precise.

    If a subroutine is defined after it is called, the call has to use parenthesis. If it doesn't, it's interpreted as a string literal (or as an error if strict subs are in effect).

    $ perl -E 'say 1, foo; sub foo { 3 }' 1foo $ perl -E 'sub foo { 3 }; say 1, foo' 13 $ perl -E 'say 1, foo(); sub foo { 3 }' 13

    However it the call uses parenthesis, there's no need it has to be know at compile time at all:

    $ perl -E 'eval "sub foo { 3 }"; say 1, foo();' 13

    But I think for this discussion it's more important to ask when the lexical pad of sub a is being built and destroyed. As long as the a is not part of the call chain, $x is in no lex pad, and sub b can't work in a meaningful way.

    Perl 6 - links to (nearly) everything that is Perl 6.

      If a subroutine is defined after it is called, the call has to use parenthesis.

      Not quite. If a subroutine is declared after it is called, the call has to use parenthesis. You can always define it afterwards.

      >perl -wle"sub foo; foo; sub foo { print 'Hi!' }" Hi!

      Granted, it's very rare for a subroutine to be declared somewhere other than when it's being defined.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2014-09-17 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (56 votes), past polls