On 1 Jul 2003 at 17:15, Vladi Belperchinov-Shabanski wrote:

> this one:
> 
> use strict;
> 
> for(1..3)
>   {
>   my $id = 1 if $_ == 3;
>   print "[$id]\n";
>   $id = 999;
>   }

This is just another instance of what I've posted about [on c.l.p.m] a 
few times, "cheap static variables".  Perl's mechanism for static 
variables is very elegant and very powerful, but is pretty syntactically 
ugly and generally is more than you want.  So if you just do:

   sub x
   {   my $staticvbl if 0 ;
        ....
   }

You now have a simple static variable -- the 'my' gets compiled at 
compile time, so you get a variable and 'use strict' is happy... BUT: 
since the 'my' isn't *executed* at run time, you end up with the effect 
of a static variable.

For example:
   sub x
   {   my $staticvbl if 0 ;
       $staticvbl += 1 ;
       print "$staticvbl\n" ;
   }
   x();
   x();
   x();

prints:
1
2
3

  /Bernie\

-- 
Bernie Cosell                     Fantasy Farm Fibers
mailto:[EMAIL PROTECTED]     Pearisburg, VA
    -->  Too many people, too few sheep  <--       



Reply via email to