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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |