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

Re: my if?

From:
Vladi Belperchinov-Shabanski
Date:
July 1, 2003 08:07
Subject:
Re: my if?
Message ID:
20030701180415.38b521bd.cade@biscom.net

yes I see this funny usage :) but I rather
have both behaviours (compile+runtime) merged
somehow...

looks like this only makes sense if

my $var = expr;

is allowed, and

my $var = expr if/unless cond;

forbidden... or replaced with `?:'

my $var = cond ? expr : undef

or even

my $var = cond ? expr; # invalid

:)))

anyway I got this shade of gray, thanx. :)

On Tue, 01 Jul 2003 10:40:26 -0400
"Bernie Cosell" <bernie@fantasyfarm.com> wrote:

> 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:bernie@fantasyfarm.com     Pearisburg, VA
>     -->  Too many people, too few sheep  <--       
> 
> 
> 
> 


-- 

*** WARNING: PLEASE DO NOT WRITE ME AT CADE@MAIL.BG! ***
MAIL.BG  SERVICE IS BROKEN AND DROPS OR REFUSES MESSAGES

--
Vladi Belperchinov-Shabanski <cade@biscom.net> <cade@datamax.bg>
Personal home page at http://soul.datamax.bg/~cade
DataMax SA   http://www.datamax.bg
Welcome to this crazy world, welcome to the show that never ends...



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