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

Replies are listed 'Best First'.
Re (tilly) 1: Filter::Static
by tilly (Archbishop) on Jan 27, 2002 at 03:06 UTC
    Ouch.

    Tie::Static was a first pass which is at version 0.01 for a reason. I created a second pass at Tie::Static take 2 with unfortunately a somewhat incompatible (but much better) API, but then got bogged down while writing tests and never released that to CPAN. (Like a fool I thought that nobody was using it.)

    If you want to release something relying on that, I should release the second version first... :-(

      Your functional interface provides for easy syntax. So please upload your take 2, so there's no need for Filter::Static at all :)

      Update - or maybe the filtered one will still be easier to use. I'm now thinking of adding a counter so multiple statics can be defined on a single line, and I think the filtered version allows for more perlish syntax. Hmmm... what do you think?
      Update2 - as far as I can see, the tied interface didn't change, which would mean I can upload the filter before you upload your new version - Or did I fail to see some obvious change?

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

        The API changes were fairly minor. If you pass additional arguments to the tie you can define any kind of sharing behaviour you want between different scopes. The old one used @_ only to allow you to have multiple ties on the same line. (I take it you encountered this?)

        As for additional comments, I think it would be bad for both of us to be making available something named static that do rather different things. One or the other of us needs to rename theirs. I don't care which, but as they stand the APIs conflict in a non-trivial way.

Re: Filter::Static
by particle (Vicar) on Jan 27, 2002 at 05:49 UTC
    there's a pod error here that (i believe) has been overlooked. you'll find that the block

    =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.
    is defined twice. i hope you catch this before you submit to CPAN!

    ~Particle

Re: Filter::Static
by BrentDax (Hermit) on Jan 28, 2002 at 10:04 UTC
    I'd suggest you use a syntax like my $var :static to match the current "attributes" syntax.

    =cut
    --Brent Dax
    There is no sig.