develooper Front page | perl.fwp | Postings from July 2003

Re: my if?

From:
Brian Duggan
Date:
July 3, 2003 01:42
Subject:
Re: my if?
Message ID:
20030702165115.A5116@seniorbridgefamily.com
On Tuesday, July  1 at 10:40 AM, Bernie Cosell wrote:
> What I don't understand is why 
> the powers-that-be provide so much resistance to putting in a simple 
> 'static' declaration that would work the same way, only be defined-and-
> legal.  e.g.,:
>    sub x
>    {   static $vbl ;
>         [...]
> 
> Oh well, I guess re-opening that wound isn't much fun...

But, that example sounds kind of fun to me :

package mystatic;
{   my %vars;
    sub TIESCALAR { my $key = $_[1]; $vars{$key} = undef unless exists $vars{$key}; bless \$key,$_[0]; }
    sub FETCH { $vars{${$_[0]}} };
    sub STORE { $vars{${$_[0]}} = $_[1]; } 
}

package main;
sub static (\$) {
    my @caller = caller 1;
    tie ${$_[0]},'mystatic',"$caller[3]::$_[0]";
}

sub x { static $vbl; print ++$vbl; }
sub z { static $vbl; print ++$vbl; }

x; x; z; z;

output: 1212

Or 'static my $vbl' would satisfy strict refs.

-Brian




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About