I've found scoping named subroutines useful when I want to initialize a data structure outside the subroutine but only visible to that one (or really as many as I want).
... { my @data = qw( this isn't visible elsewhere ); sub func { # Do something with @data here ... } } ...

You could do just as well inside another subroutine instead of just a bare block (@data is private to both subroutines), then you're working with closures. Typically though the inner subroutine is spawned anonymously and returned from the outer sub. For a good example of that see List::MoreUtils's natatime.

Seeing as how others have pointed out the inner sub is not really purposefully scoped (you redeclare your variable with 'my'), I'm guessing at what you're asking about. Higher Order Perl is a good book and one that I've been meaning to finish which talks all about closures and other things you can do with higher order functions.


In reply to Re: Inner subroutines? by blakew
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.