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

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

Thread Previous | Thread Next
Jim Cromie
November 30, 2003 14:53
Re: [DOCPATCH] BEGIN, CHECK, INIT, END explained more
Message ID:
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.

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.

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()
  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.

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

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

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)

FWIW, BEGIN and END can be nested, though I dont know whether this
belongs in the begincheck example recently proposed in *, and added

    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.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About