Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

a sub within a sub -- revisiting

by punkish (Priest)
on Apr 10, 2010 at 03:59 UTC ( [id://833941]=perlquestion: print w/replies, xml ) Need Help??

punkish has asked for the wisdom of the Perl Monks concerning the following question:

I read Defining a sub within a sub: OK? with interest, but consider the following

sub foo { my @arra = ('a' .. 'z'); my @arrb; for ( 0 .. ( $#arra - 1) ) { bar($_); } bar($#arra); sub bar { my ($i) = @_; push @arrb, $arra[$i]; } return \@arrb; }

The above works, but what all sentiments and principles am I violating here? (besides, of course, Blond highlights with a hint of purple are just "so 2007

Essentially, bar() will never be called from outside foo(), and makes sense only within the context of foo(). In case I shouldn't be doing the above, then how should I rewrite it?

--

when small people start casting long shadows, it is time to go to bed

Replies are listed 'Best First'.
Re: a sub within a sub -- revisiting
by ikegami (Patriarch) on Apr 10, 2010 at 04:54 UTC

    The above works

    No, it doesn't. Call foo twice and check the results. (It always returns undef after the first call.)

      Just want to add one point to make this observation complete: this issue can be fixed by doing what the anonymous monk suggested above.

      However, the best is to code in a simple and straight forward manner. That's the principle.

      Peter (Guo) Pei

Re: a sub within a sub -- revisiting
by Anonymous Monk on Apr 10, 2010 at 04:14 UTC
    bar is still visible outside of foo, so make it lexical
    sub foo { my $bar = sub { my ($i) = @_; push @arrb, $arra[$i]; }; $bar->($#arra); ... }

      Note that the above leaks if the anon sub accesses $bar. That would be the case if the anon sub is recursive, for example. I find the following simpler, and it doesn't leak.

      sub foo { ... local *bar = sub { ... }; bar(...); }
Re: a sub within a sub -- revisiting
by PeterPeiGuo (Hermit) on Apr 10, 2010 at 05:29 UTC

    Good code is usually straight forward, and this code is misleading in couple of ways:

    1. Visually, bar seems to be only available inside foo. But that's not true, bar can actually be called outside of foo.
    2. Make it even worse, when bar is called outside of foo, it does not do what's expected any more.

    Peter (Guo) Pei

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-19 02:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found