Incidently, why do you want a sub sub? Is it so your sub can access private variables, or so your sub can use a routine name that may already be in use, or what? Just curious.
One of the nice things about block-local sub definitions is that they stay local to that block. In other words, you only have to understand that sub in the context of the block it's declared in, which makes a lot of sense when that's the only place it gets used.
For example, I have some C code that reads a TARGA image. Obviously, I don't want to do everything in a single function, so I break it up into one user-visible routine (read_tga_file) and two helper routines (read_tga_header and read_tga_image). The two helpers only ever get called within read_tga_file, so why should they be part of tga.o's global symbol space? If I could declare the helpers within read_tga_file's symbol space, anyone reading the file would know that they only get used there. That's a win, especially when the helper functions are a bit less obviously specialized. Lexical function declarations let you encode some of your assumptions in source code.
This idea of "declare stuff in the smallest enclosing lexical block" is ubiquitous in the functional-programming world. As usual, Perl lets you do it with minimal fuss.
Found a typo in this node? /msg me
% man 3 strfry
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
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:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||