Hello there,

Static variables (and also Perl 6) inspired me to create a filter to ease the use of tilly's great Tie::Static.
If you have any objections to this being on CPAN in its current form, hit the comment link below :).

Update - Added a counter so multiple declarations can be on a single line. Added some documentation about tilly's new version that has yet to be uploaded.
Update2 - Fixed Tie::Static 0.01/0.02 incompatibility

package Filter::Static; use Tie::Static; use Filter::Simple; use strict; use vars qw($VERSION); $VERSION = '0.01'; my $variable = q/[$@%]\w+/; FILTER_ONLY code => sub { while (/static\s*(?=\(\s*($variable(?:\s*,\s*$variable)*)\s*\) +)/o or /static\s*(?=($variable))/o) { my @vars = split /\s*,\s*/, $1; s[] { join '', map { "tie my $_, 'Tie::Static', " . uniq() . ';' } @vars }e; } }; my $counter = 0; sub uniq () { return $Tie::Static::VERSION < 0.02 ? $counter++ : 'Filter::Static::me(), ' . $counter++; } sub me () { caller } 1; __END__ =head1 NAME Filter::Static - Add a "static" keyword =head1 SYNOPSIS use Filter::Static; sub counter { static $count; return ++$count; } @foo = (counter, counter, counter); # @foo = qw(1 2 3); sub examples { # Single value, using a default static $drinks = 3 unless defined $drinks; # Single value, using a default when it's false static $greeting ||= "Hello" # Single value, no default (undef) static $counter; # Single value, ALWAYS set to 3 (useless) static $uselessness = 3; # Multiple values, no default static ($first, @second); # Multiple values, setting defaults if $foo is true static ($foo, @bar) = (0, 1, 2, 3) if $foo; # Multiple values, ALWAYS set (useless) static ($moo, $bleat) = qw(cow sheep); } =head1 DESCRIPTION This module uses Filter::Simple and Tie::Static to add a C<static> keyword. C<static> has to be the first word in the statement, or it will fail. In the statement, the values (that are tied lexicals) can be used immediately (allowing for defaults to be set using C<if> and C<unless>) because the statement is run with the word C<static> removed. Only pieces that match a list of normal variables or a single variable are used - that's why C<static $foo++> will work, and C<static ++$foo> won't. Tie::Static 0.02 has a functional interface. You can use that one together with Filter::Static, because the backslash that creates the reference breaks the filter's pattern match. TIMTOWTDI++ { my $foo; sub bar { } } # No module tie my $foo, 'Tie::Static'; # Tie::Static static \ my $foo; # Tie::Static static $foo; # Filter::Static =head1 BUGS =over 2 =item * All bugs that Filter::Simple and Tie::Static have, are of course shared by this module. This module will allow you to have several C<static> statements on a single line. =item * Symbolic references and variable names including punctuation cannot be used. =item * Quotelikes are not filtered, so C<static> can not be C<eval>ed. =back =head1 AUTHOR Juerd <juerd@juerd.nl> =head1 SEE ALSO L<Filter::Simple>, L<Tie::Static> =cut

In reply to Filter::Static by Juerd

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.