Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion is static?

Newsgroups: perl.perl6.language
Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.perl.org
Return-Path: <aar...@gbdirect.co.uk>
Mailing-List: contact perl6-language-h...@perl.org; run by ezmlm
Delivered-To: mailing list perl6-langu...@perl.org
Delivered-To: perl6-langu...@perl.org
Date: Tue, 18 Mar 2003 12:14:05 +0000
To: perl6-langu...@perl.org
Subject: Re: is static?
Message-ID: <20030318121405.GB29022@gbdirect.co.uk>
Mail-Followup-To: perl6-langu...@perl.org
References: <x7of4c7cj0.fsf@mail.sysarch.com> <slrnb799pr.28h.Smylers@stripey.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <slrnb799pr.28h.Smylers@stripey.com>
User-Agent: Mutt/1.4i
Mail-Copies-To: nobody
X-SMTPD: qpsmtpd/0.25, http://develooper.com/code/qpsmtpd/
X-Spam-Check-By: one.develooper.com
X-Spam-Status: No, hits=-1.9 required=7.0 tests=CARRIAGE_RETURNS,IN_REP_TO,QUOTED_EMAIL_TEXT,REFERENCES,SPAM_PHRASE_05_08,USER_AGENT,USER_AGENT_MUTT version=2.44
X-SMTPD: qpsmtpd/0.25, http://develooper.com/code/qpsmtpd/
Approved: n...@nntp.perl.org
From: aar...@gbdirect.co.uk (Aaron Crane)
Lines: 43

Smylers writes:
> I don't find the Perl 5 approach ugly: I actually like it, because it does
> exactly what it looks like it's doing, and doesn't require learning any
> special syntax or keyword.
> 
> To have a variable exist beyond outside a sub, you declare it outside that
> sub.  To prevent that variable being accessed from anywhere else, you put
> it in a block.  It's simple yet provides the power you need, and it's
> completely obvious what's going on.

I disagree that it's simple.

  #! /usr/bin/perl -lw
  use strict;

  print id();

  {
      my $next = 17;  # the first ID is 17
      sub id {
          return $next++;
      }
  }

Unfortunately, that completely fails, because the block containing the
declarator hasn't been evaluated the first time you call the sub.  So you
get 0 as the answer (because ++ is sufficiently magic).  It can be fixed by
making the block a BEGIN block, but suddenly it doesn't seem so simple.  It
doesn't help that when you do this in a module, you probably don't see the
problem (because 'use Foo;' effectively does a require in a BEGIN block).

I'd argue that the requirement for BEGIN when you want a so-called-static
variable in your main program (and you define the sub after using it) makes
this approach less than simple.

In addition, I don't think it 'provides the power you need'.  With the Perl5
approach, you can't have so-called-static variables scoped to anything other
than (a group of) subroutines -- they can't be scoped to a loop within a
sub, for example.

-- 
Aaron Crane * GBdirect Ltd.
http://training.gbdirect.co.uk/courses/perl/