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 29, 2003 16:40
Subject:
Re: [DOCPATCH] BEGIN, CHECK, INIT, END explained more
Message ID:
p05111b03bbeeea7885d2@[192.168.56.3]
At 23:15 +0000 11/29/03, Arthur Bergman wrote:
>On Saturday, November 29, 2003, at 10:15  pm, Elizabeth Mattijsen wrote:
>>$ perl -MO=Deparse -e 'BEGIN { foo => "bar" }'
>>sub BEGIN {
>>     'foo', 'bar';
>>}
>>-e syntax OK
>>
>>$ perl -MO=Deparse -e 'CLONE { foo => "bar" }'
>>do {
>>     'foo', 'bar'
>>}->CLONE;
>>-e syntax OK
>Because CLONE/DESTROY are called on objects, the mere presence of 
>them does not affect the compilation.

Hmmm... that statement as such is incorrect, I think.  CLONE is 
called as a _class_ method (I just realized), _not_ as an object 
method.  As the following shows:

  use threads;
  sub CLONE { warn "CLONE called with @_\n" }
  threads->new( sub { 1 } )->join;

gives as output:

  CLONE called with main

This is a point that should be made more clearly.  I just realised 
that after really grokking the "or inherited" in the following text 
from perldelta.pod:

Support for the C<CLONE> special subroutine had been added.
With ithreads, when a new thread is created, all Perl data is cloned,
however non-Perl data cannot be cloned automatically.  In C<CLONE> you
can do whatever you need to do, like for example handle the cloning of
non-Perl data, if necessary.  C<CLONE> will be executed once for every
package that has it defined or inherited.  It will be called in the
context of the new thread, so all modifications are made in the new area.

Boy, is that really hidden!  Not so much the fact that it is called 
as a class method, but that it is inherited.  I always thought of 
CLONE as a BEGIN, but I should have thought of CLONE as a DESTROY 
(sort of).  I never realized it was doing that.  Must check my 
Thread::xxx modules to see whether it all works as I though it did... 
;-(


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