|
Posted by pgodfrin on March 27, 2008, 2:05 pm
Please log in for more thread options
> > OK - I can stay away from INIT, CHECK and UNITCHECK. But your point
> > about learning about initialization, compile vs runtime and
> > destruction is pretty much what I'm trying to do - learn about it. So
> > I will re-iterate my question - is there any information out there -
> > an article, a book, perl's documentation - that discusses:
>
> > How exactly would you use BEGIN and END code blocks for
> > initialization, compile time, runtime and destruction issues?
>
> BEGIN is there to run code before the later code gets compiled. This is
> useful in a few cases, mainly to manually define subroutines & packages
> up-front so that the perl compiler knows about them (indirect object
> notation relies on this, IIRC). It may also be useful to force some
> initialization before use()ing a module that relies on it.
>
> END blocks are useful to "guarantee" that code gets run when the program
> ends, even if for example an exception is thrown. Useful for system
> resources that may not get freed properly otherwise.
>
> Those two are what you normally need. The others are more esotheric.
>
> INIT, CHECK and UNITCHECK blocks are there to be able to manipulate the
> compiled op tree before it gets run and they're IIRC mainly used by the
> B backend modules. See for instance, perlguts, B and O.
>
> > I've been using END as a centralized exit point, so that when my code
> > executes die() or exit() I can still cleanup and do other things. Is
> > there more to know about that?
>
> Not really.
>
> > I also came across a glitch when using CGI and DBI modules, where some
> > variables lose scope when the BEGIN code block is completed (which is
> > clearly alluded to in perlmod). But in plain old Perl I can still
> > refer to variables assigned in BEGIN in later parts of the code. So
> > there is a corollary between these code blocks and scope. It would
> > seem to me that a good discussion about scope (perldoc ???) is in
> > order too...
>
> I think you're mistaken, that always happens:
>
> $ perl -Mstrict -w -e'BEGIN { my $f = 1;} print $f'
> Global symbol "$f" requires explicit package name at -e line 1.
>
> HTH,
> Joost.
>
> --
> Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
Thanks Joost, I will look at the perlguts doc and cleanup my
concepts...
regards,
pg
|