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 Ilya Zakharevich on March 29, 2008, 4:49 pm
Please log in for more thread options
[A complimentary Cc of this posting was sent to
Bart Lateur
> 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.

You mean "Fortunately they are not called on _exit(); do not forget to
never use exec()"?

[END() IS called on exit(). Signal handlers can be installed to die()
on signal, so END() is called on signals.]

Hope this helps,
Ilya

Posted by Ben Morrow on March 29, 2008, 6:30 pm
Please log in for more thread options

> [A complimentary Cc of this posting was sent to
> Bart Lateur
> > 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.
>
> You mean "Fortunately they are not called on _exit();

Well, no, that's kind-of the point...

> do not forget to never use exec()"?

I am confused. How is one (on a fork/exec-based system, or perl's
emulation of such under Win32) supposed to avoid calling exec when
necessary?

> [END() IS called on exit(). Signal handlers can be installed to die()
> on signal, so END() is called on signals.]

END blocks are not called when a process exits because of the DEFAULT
action of a signal. It is of course possible to fix this by installing
an appropriate signal handler.

Ben


Posted by Joost Diepenmaat on March 29, 2008, 6:45 pm
Please log in for more thread options

> I am confused. How is one (on a fork/exec-based system, or perl's
> emulation of such under Win32) supposed to avoid calling exec when
> necessary?

By using fork(), ofcourse! Am I missing something?

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

Posted by Ben Morrow on March 29, 2008, 8:56 pm
Please log in for more thread options

>
> > I am confused. How is one (on a fork/exec-based system, or perl's
> > emulation of such under Win32) supposed to avoid calling exec when
> > necessary?
>
> By using fork(), ofcourse! Am I missing something?

Err... yes? fork creates a clone of the current process, exec changes
which file the current process is executing. Any method of executing an
external program, on Unix, ends up calling exec.

Ben


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

>>
>> > I am confused. How is one (on a fork/exec-based system, or perl's
>> > emulation of such under Win32) supposed to avoid calling exec when
>> > necessary?
>>
>> By using fork(), ofcourse! Am I missing something?
>
> Err... yes? fork creates a clone of the current process, exec changes
> which file the current process is executing. Any method of executing an
> external program, on Unix, ends up calling exec.

But for the purposes of END blocks, using fork() before an exec() would
allow the END blocks to run anyway:

# simplified:

END {
# clean up stuff here
# assuming close-on-exec & equivalents are correctly set
}

if (! fork()) { # assumin fork() doesn't fail here.
exec $whatever;
exit;
}
exit;

This is assuming the fork() or exec() themselves won't introduce new
issues.

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