Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Pkg::END{} executes 2+ times, on how many times use Pkg; or do Pkg.pm;

by petr999 (Acolyte)
on Jan 22, 2010 at 16:35 UTC ( [id://818994]=perlquestion: print w/replies, xml ) Need Help??

petr999 has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I have the END{} block on some module.
It is use()d by some script, to 'todo' script.
The 'preload' script does the do() on it. Both execute the code in the context of the package main;
The 'preload' script fork()s then and the compiled module's machine code is shared among the forks.
The problem is: if I put the 'use module;' or 'do "module.pm";' on the 'preload' script no matter if it is before the do() of the todo.pl or after, the END{} of the module gets to be executed twice.
I suppose there should be the built-in variable that keeps the used modules each time they use to be loaded. This is not the %INC because the module.pm is the key of that hash.
There is certainly a condition on the rest of the code that influenced the code to behave like that, I could not reproduce all of that at the moment being with the vanilla plain module, todo and preload files to put them here as a simple sample of the code. Although the reproduceable code itself is a no secret at all. I am sure it should be obvious for some kind Monk to know such a stuff. ;-)
Thank you.
Peter Vereshagin peter@vereshagin.org http://vereshagin.org
  • Comment on Pkg::END{} executes 2+ times, on how many times use Pkg; or do Pkg.pm;

Replies are listed 'Best First'.
Re: Pkg::END{} executes 2+ times, on how many times use Pkg; or do Pkg.pm;
by Corion (Patriarch) on Jan 22, 2010 at 16:41 UTC

    If you don't want to load code twice, use use or require. Using do is a bad idea as you found, as it will re-execute the code every time, and add the END{} blocks every time as well.

Re: Pkg::END{} executes 2+ times, on how many times use Pkg; or do Pkg.pm;
by JavaFan (Canon) on Jan 22, 2010 at 16:44 UTC
    If you have a fork() both the parent and the child will execute the END.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://818994]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2025-06-21 18:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.