I don't recall any of the discussions of Perl 6 mentioning static variables. There aren't any good ways to get static variables in Perl 5. The best is probably:

{ my $static; BEGIN { $static= "Initial value" } sub usesStatic { # ... } }
but that won't work under mod_perl (and, well, it also sucks quite a bit).

Several times I've mentioned that I'd like to see BEGIN (and a few other similar "keywords") be supported as statement modifiers, in part so that we could have real static variables in Perl:

sub usesStatic { my $static= "Initial value" BEGIN; # ... }
but never with much visibility.

[

Note that this would function quite a bit like

sub usesStatic { my $static if 0; # ... }
but without being a horrid hack that many consider to be bug (and you could provide an initial value and Perl would be smart enough to make $static so that it does "stay shared" if you used nested subroutines for some strange reason such as mod_perl).

]

I really like that the BEGIN makes it clear that the initialization code is going to run at compile time and that you could instead say, for example, CHECK if you want the initialization to happen a bit later.

So I wanted to raise the visibility of this idea. I also was hoping someone had some insight into what plans, if any, there are for static variables in Perl 6. Depending on the feedback I get here, I may post this suggestion to a Perl 6 discussion list.

Note that you could also use this for other things. If there was also a way to use eval without introducing an extra layer of scope, then you could do lots of really fun things akin to source-code macros:

eval q{ conditional code; } if condition BEGIN;
where the "conditional code" could, for example, declare lexicals, which could be really cool (and dangerous).

Update: Note that global variables ("package" variables) are also a form of static variable and they have some advantages ove the "best" version I provided above. They also have some disadvantages (being global), otherwise the "best" version would have never been thought up. Feel free to think "non-global static" or "static lexical" whenever you read "static" above if that helps. (:

        - tye (but my friends call me "Tye")

In reply to Static variables (and also Perl 6) by tye

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.