develooper Front page | perl.perl5.porters | Postings from November 2003

Re: [DOCPATCH] BEGIN, CHECK, INIT, END explained more

Thread Previous | Thread Next
From:
Elizabeth Mattijsen
Date:
November 30, 2003 15:19
Subject:
Re: [DOCPATCH] BEGIN, CHECK, INIT, END explained more
Message ID:
p05111b05bbf0269172ae@[192.168.56.3]
At 15:53 -0700 11/30/03, Jim Cromie wrote:
>Elizabeth Mattijsen wrote:
>>A special section may need to be added later, to show that only 
>>BEGIN, CHECK, INIT and END don't need the "sub" prefix, and the 
>>other all uppercase subroutines _do_ need it (even if they don't 
>>generate any errors during compilation).
>1st, it may be wise to refer to these as "SPECIAL", not MAGIC,
>since latter has a specific meaning in perl.  Whether the specific
>'magic' meaning should be reserved in perlmod.pod is a question
>for the 'editor', as the audience is certainly broader than perlguts.

I guess the magic is really in the parser than anything else...  I 
could live with SPECIAL as indeed MAGIC has a special annotation.  By 
the way, the original title of the section in perlmod.pod was:

   Package Constructors and Destructors

which I though was an even more confusing description of BEGIN and END blocks.


>wrt classifying the specialness, these are the apparent critera;
>whether theyre a useful or suboptimal taxonomy is something Ill learn
>from following this thread ..
>1.   whether its callable by a user
>    BEGIN, END, CHECK, INIT are not.

Actually, they are, if you create a "real" subroutine with that name, e.g.

  $ perl -e '*BEGIN = sub { warn "Hello world!\n" }; &BEGIN'
  Hello world!
  $

Again, the magic of BEGIN, CHECK, INIT and END is in the parsing and 
the handling of the result of the parsing.  After that, it's all 
"normal"...  ;-)


>btw, AUTOLOAD is callable directly, tho Im not sure it should be.
>following example shows that a direct call gets a stale $AUTOLOAD.
>
>  DB<1> sub AUTOLOAD {print "foo\n"}
>  DB<2> joe()
>foo
>  DB<5> sub AUTOLOAD {print "Auto: $AUTOLOAD @_\n"}
>  DB<8> $a = AUTOLOAD('bar')
>Auto: main::joe bar
>maybe it should be magically ;-) cleared by a goto &NAME or return.
>Then again, it could be a 'dont do that then' type buglet.

Not sure what you're doing here: the following shows that $AUTOLOAD 
is not staled:

  $ perl -e 'sub AUTOLOAD { warn "Hello world: $AUTOLOAD\n" }; 
$AUTOLOAD = 'foo'; &AUTOLOAD'
  Hello world: foo


>CLONE() is callable, (assuming this test is valid)
>but is meant/reserved for threading & multiplicity.
>  DB<1> sub CLONE {print "clone: @_\n"}
>  DB<2> $a = CLONE(1)
>clone: 1

  $ perl5.8.2-threaded -e 'sub CLONE { warn "Hello world!\n" }; &CLONE'
  Hello world!
  $ perl5.8.2-unthreaded -e 'sub CLONE { warn "Hello world!\n" }; &CLONE'
  Hello world!

Just an ordinary sub, even in unthreaded Perls.


>2.  Whether its called magically by CORE on your behalf.
>this is almost the opposite of 1, but for CLONE and AUTOLOAD

Well, I would put CLONE in the same class as the perltie special 
subs, and the ones in PerlIO::via.  It only gets called when you do a 
threads->new, just as the tie methods get called if you do a tie().


>3. Whether theyre Methods or Functions.
>tie methods are, duh, Methods.
>those in 1 are Functions, except for AUTOLOAD, which should be
>used as a method (authors choice though).
>This criterion may not be important enough to draw here, since
>they mostly go both ways.
>
>4. whether theyre called by require, eval {}, eval ""
>and whether theres a distinction within these - I think so..
>As I (mis)? understand it, INIT and CHECK are only called at compilation
>of main, (use time only).  I thought this was confusing when I read it
>(cant find a citation)

CHECK and INIT are only run after the initial "eval" (if you will) is 
completed.


>FWIW, BEGIN and END can be nested, though I dont know whether this
>belongs in the begincheck example recently proposed in *, and added 
>http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2003-11/msg00508.html

Oops, missed that when it was posted.  I think it is a nice example, 
but I would point out that the number in the text, is actually the 
ordinal number of the message as it will appear on the screen.


>BEGIN {
>    print "first begin\n";
>    BEGIN {
>    print "first begin nested \n";
>    }
>    END {
>    print "nested end\n";
>    }
>}
>...
>
>first begin nested
>first begin
>2 begin nested
>2nd begin
>begin nested in end
>in main
>1st end
>
>If you think further additions along these lines are worthy in 
>begincheck, Ill do a patch.
>If you think they belong in a separate init-check-require-eval 
>example, I can do that instead.

I would think a whole seperate pod with a list of all uppercase 
"subroutines", in which class they fall and when they are called, 
would be nice to have.  A sort of perlALLCAPS.pod ?


Liz

Thread Previous | Thread Next


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