On Mon, Oct 23, 2000 at 08:27:52PM -0400, Dan Sugalski wrote:
> At 12:33 AM 10/24/00 +0100, Simon Cozens wrote:
> >On Mon, Oct 23, 2000 at 03:40:26PM -0700, Peter Scott wrote:
> > > >Don't forget that those BEGIN blocks are *supposed* to be instructions
> > > >to the compiler.
> > >
> > > Er, but a lot of people seem to use them for other things :-)

cos perl 5.004 or so didn't have INIT blocks and so BEGIN is doing 2 jobs:
hints & instructions to the parser & compiler, and run time initialisation.

> A lot of the tiny programs aren't all that tiny, either. Ten lines isn't 
> much unless one of them's "use CGI;" or "use Net::FTP". And there are huge 
> gobs of those.

while use ...; maps to
BEGIN {
  require ...;
}

we still potentially have both compiler (directives & declarations) and
run time (initialisation) in the one place. perl 6 module writers will
have to get good at putting blocks of module inside INIT blocks.

Maybe the perl5 to perl6 convertor will be taking a module that looks like
this

package locale;

$locale::hint_bits = 0x800;

sub import {
    $^H |= $locale::hint_bits;
}
sub unimport {
    $^H &= ~$locale::hint_bits;
}
1;



and turning them into:

package locale;

our $locale::hint_bits;

INIT {
  $locale::hint_bits = 0x800;
}

sub import {
    $^H |= $locale::hint_bits;
}
sub unimport {
    $^H &= ~$locale::hint_bits;
}



so that definitions and declarations are still BEGIN time (which happens
once in a program's life when it gets compiled), but initialisations are
moved to INIT time (which happens 0 to lots of times; ie once per running)

Hmm. I was about to say "Except that maybe that should be:

our $locale::hint_bits = 0x800;

" but I think not; even in perl5 it doesn't get initialised until the
code runs when the scope ends at the "1;" Modules that did

BEGIN {$locale::hint_bits = 0x800;}

still do what's expected of perl5 when written:

INIT {
  BEGIN {$locale::hint_bits = 0x800;}
}

Nicholas Clark

Reply via email to