in reply to Defining a sub within a sub: OK?
As others have pointed out already there's no sub inside a sub in your code. I'll answer the question asked in your title though:
I consider it a bad idea to define a named sub within another sub. It's visible from the outside still, but obeys rather unintuitive scoping rules:
use strict; use warnings; sub a { my $x = 3; sub b { print $x, $/; } } b(); __END__ Variable "$x" will not stay shared at foo.pl line 7. Use of uninitialized value $x in print at foo.pl line 7.
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Defining a sub within a sub: OK?
by Joost (Canon) on Oct 14, 2009 at 22:52 UTC | |
by moritz (Cardinal) on Oct 14, 2009 at 23:06 UTC | |
by ikegami (Patriarch) on Oct 15, 2009 at 01:19 UTC | |
|
Re^2: Defining a sub within a sub: OK?
by ikegami (Patriarch) on Oct 15, 2009 at 00:07 UTC | |
by AnomalousMonk (Archbishop) on Oct 15, 2009 at 08:08 UTC | |
by ikegami (Patriarch) on Oct 15, 2009 at 12:49 UTC |