[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] Re: RFC: lexical variables made default (revised)



Nathan Wiger <nate@wiger.org> writes:

> > You've taken the wrong approach.  If you're writing a big program then
> > there should be *no* default scope.  Any variable access is an error
> > unless that variable was my()ed or our()ed.  That's basically what
> > 'strict' gives us.
> 
> Fair enough. I've heard several good analyses against my idea, so I'll
> fold. Notice I never claimed it was a *good* idea (hence the lack of
> RFC), but just an idea worth pondering.
> 
> However, I do think there's something to be said for a "quick-and-dirty"
> script out of the box that can distinguish between sub{} vars and other
> vars ala C:
> 
>    $user = 'nwiger';
>    sub whois {
>        ($user) = @_;    # different var
>        # ...
>    }
>    print whois($user);

Uhmm, but now you've killed off *any* chance of whois (or any other
sub) *ever* setting $user -- the first time it tries to do that, it
gets a `different var' and the original remains unchanged.

In particular, one of Perl's greatest advantages over Python -- the
existence of real closures -- softly and silently vanishes away.
Consider this code (Perl/Tk code tends to be similar):

   sub make_summer {
     my $x = 0;
     sub { $x += shift }
  }

Then we can say:
  DB<2> $a = make_summer

  DB<3> $b = make_summer
 
   DB<4> x $a->(2)
  2
  DB<5> x $a->(3)
  5
  DB<6> x $b->(11)
  11
  DB<7> x $b->(-1)
  10

But it is impossible to write make_summer with your suggestions in
effect!  Suddenly, the `$x' in the anonymous sub make_summer is trying 
to create becomes a *new* variable, and all state goes away.

What you're proposing seems to be the abolition of *all* scopes from
Perl, except the innermost-sub scope.  This is worse than Pascal.

But here's an idea: suppose you spelt `my' V-A-R.  Then you could say
Perl is forcing you to declare all your variables (which I claim is
essential in a language with useful scopes).  Of course, it's one
character longer, so I suppose `var' is out of the question...

[...]

-- 
Ariel Scolnicov        |"GCAAGAATTGAACTGTAG"            | ariels@compugen.co.il
Compugen Ltd.          |Tel: +972-2-6795059 (Jerusalem)	\ We recycle all our Hz
72 Pinhas Rosen St.    |Tel: +972-3-7658514 (Main office)`---------------------
Tel-Aviv 69512, ISRAEL |Fax: +972-3-7658555    http://3w.compugen.co.il/~ariels

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe