Effectively, that code is identical to this:

use strict; use warnings; use 5.010; use feature qw(say); sub inner_sub { my $param = shift; say "Start inner_sub1"; return ++$param; } sub outer_sub { say "Outer Subroutine Start ..."; my $param = 0; say "PARAM: $param"; $param = inner_sub($param); say "PARAM: $param"; say "Outer Subroutine End."; } # Main outer_sub(); exit(0); __END__ [22:16:28.33] c:\test>junk41 Outer Subroutine Start ... PARAM: 0 Start inner_sub1 PARAM: 1 Outer Subroutine End.

The fact that inner_sub() is enclosed in outer_sub() makes no difference to its visibility:

use strict; use warnings; use 5.010; use feature qw(say); sub outer_sub { say "Outer Subroutine Start ..."; my $param = 0; say "PARAM: $param"; $param = inner_sub($param); say "PARAM: $param"; say "Outer Subroutine End."; sub inner_sub { my $param = shift; say "Start inner_sub1"; return ++$param; } } # Main outer_sub(); inner_sub(); ### Visible here also! exit(0); __END__ [22:20:34.37] c:\test>junk41 Outer Subroutine Start ... PARAM: 0 Start inner_sub1 PARAM: 1 Outer Subroutine End. Start inner_sub1

But, declaring nested sub is "dangerous" in that it can result in closures that don't work properly like this:

use strict; use warnings; use 5.010; use feature qw(say); sub outer_sub { my $scoped_var = 'fred'; ## closure say "Outer Subroutine Start ..."; my $param = 0; say "PARAM: $param"; $param = inner_sub($param); say "PARAM: $param"; say "Outer Subroutine End."; sub inner_sub { my $param = shift; say "Start inner_sub1"; say $scoped_var; ## closure return ++$param; } } # Main outer_sub(); inner_sub(); exit(0); __END__ [22:22:00.66] c:\test>junk41 Variable "$scoped_var" will not stay shared at C:\test\junk41.pl line +20. Outer Subroutine Start ... PARAM: 0 Start inner_sub1 fred PARAM: 1 Outer Subroutine End. Start inner_sub1 fred

Though I forget the circumstances in which the "Variable "$scoped_var" will not stay shared " actually causes a problem. I think it's been possible to declare inner subs for a long time, certainly as long as I've been using Perl, but AFAIK, there is no good reason for doing so, and it can--though I forget the circumstances because I never do it--cause problems.


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.

In reply to Re: Inner subroutines? by BrowserUk
in thread Inner subroutines? by wsppan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.