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/

Reply via email to