State variables (was: Re: Bug in pixbuf->copy ?)



On Tue, 2006-04-18 at 19:36 +0200, Thierry Vignaud wrote:

This is actually an undocumented and not future-proof way of getting
static variables.  The "glitches" that make it work are described in
[1].  There was also an entry about this on of the p5p digests, but the
original site[2] doesn't seem to have it anymore -- probably to not
further publicize the trick.  Anyway, the Google cache[3] still has it.

well, until state variables are availlable (perl-5.9.x ???), that's
the only way to have static variables, that do be usefull in some
cases.

Not really.  If you put a block around a sub, you can declare a lexical
variable in that scope but outside the sub.  It will then behave like a
state variable:

  {
    my $state = 0;
    sub bla {
      print $state++, "\n";
    }
  }
  bla(); # 0
  bla(); # 1
  bla(); # 2

-- 
Bye,
-Torsten




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]