The bug recently discussed in Unusual Closure Behaviour is considered something of an oddity, with little practical value, but here's what I believe is a valid and defensible use of it, specifically with regards to mod_perl, and more generally with regards to any tool that wraps scripts into callable subroutines/hooks.

Those of you who've developed for mod_perl know that it's a bad idea to create global lexical variables in your Apache::Registry scripts, since they do not stay shared when mod_perl wraps your code into a subroutine. For example, under mod_perl, the following script is transformed from:
#!/usr/bin/perl -w ... my $fname = $q->param('fname'); my $lname = $q->param('lname'); ... sub foo { my $name = $fname.$lname; ... }
into a subroutine like this:
sub handler { ... my $fname = $q->param('fname'); my $lname = $q->param('lname'); ... sub foo { my $name = $fname.$lname; ... } }
As you see, foo() becomes one of those pesky inner named subroutines, which means that upon successive invocations of the script (or more precisely, the handler subroutine), the $fname and $lname variables of the inner sub will remain bound to their values from the first invocation, and will not reflect new values. This issue is well documented in the mod_perl guide, along with several remedies for the problem, none of which I like too much.

Looking over that discussion, it occured to me that the 'my $x if 0' construct can resolve this issue:

sub OUTER { my $x if 0; $x = 0; print "OUTER: \$x is now: ", ++$x, $/; sub INNER { print "INNER: \$x is now: ", ++$x, $/; } } OUTER;INNER;INNER; OUTER;INNER;INNER; ## results OUTER: $x is now: 0 INNER: $x is now: 1 INNER: $x is now: 2 OUTER: $x is now: 0 INNER: $x is now: 1 INNER: $x is now: 2
What's happening here is that successive invocations simply overwrite the values bound upon the initial invocation. Since we've cheated Perl out of enacting the run-time effect of my $x, this allows lexical variables to stay shared between outer and inner named subroutines (despite -w's admonitions to the contrary).

I don't think I would do something like this in production code. I'll probably stick to using package globals, perhaps with our-scoping, but it's certainly worth thinking about and perhaps incorporating as a bonified language feature in future perl releases, eg:

... my $fname : static = $q->param('fname'); my $lname : static = $q->param('lname'); ...
   MeowChow                                   
               s aamecha.s a..a\u$&owag.print

In reply to It *is* a feature: mod_perl and 'my $x if 0' by MeowChow

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.