Click here to get back home

BEGIN, INIT etc...

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
BEGIN, INIT etc... pgodfrin 03-26-2008
---> Re: BEGIN, INIT etc... Gunnar Hjalmars...03-26-2008
Posted by Joost Diepenmaat on March 27, 2008, 1:09 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/

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

Posted by Gunnar Hjalmarsson on March 27, 2008, 3:27 pm
Please log in for more thread options
Joost Diepenmaat wrote:
>> 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.

So you'd better declare the variable outside the BEGIN block.

$ perl -Mstrict -w -e'my $f; BEGIN print "$f\n"'
1
$

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Bart Lateur on March 29, 2008, 9:23 am
Please log in for more thread options
Joost Diepenmaat wrote:

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

Unfortunately they're still not called on exit and on exec.

--
        Bart.

Posted by Joost Diepenmaat on March 29, 2008, 9:29 am
Please log in for more thread options

> Joost Diepenmaat wrote:
>
>>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.
>
> Unfortunately they're still not called on exit and on exec.

They are called on exit(), just not on POSIX::_exit

$ perl -w -Mstrict -e'END{ print "END"}; exit'
END

Cheers,
Joost.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Similar ThreadsPosted
Q on BEGIN and INIT April 19, 2005, 3:05 pm
Should I use BEGIN, CHECK, or INIT? October 27, 2004, 8:32 pm
CHECK and INIT May 19, 2006, 11:00 am
BEGIN <> BEGIN May 31, 2006, 5:32 am
Where to begin with Perl 6 September 5, 2005, 11:03 am
BEGIN { package Foo; use Foo } December 24, 2005, 6:49 pm
Nub question - were to begin? April 26, 2006, 2:33 pm
bless an object in a BEGIN block (Singleton) October 13, 2006, 3:02 am
BEGIN not safe after errors--compilation aborted February 22, 2008, 11:26 am
Regular expression to match any line that DOESN'T begin with a particular string March 22, 2006, 7:48 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap