J
J. Romano
Greetings,
I have a couple of questions and I would like to know the opinions
of the Perl community.
I while ago, I wrote a Perl module which is now practically
finished. Its exact contents aren't important for my questions,
except for the fact that it is a package and its functions are called
like:
use MyPackage;
MyPackage::init();
MyPackage::f1();
MyPackage::f2($var1, $var2);
The first function that must be called is an initialization
function (MyPackage::init()). It must be called before any other
function in the package and should only be called once. This fact is
well-documented in the perldoc documentation, so any Perl programmer
who bothers to check this module's documentation should be aware of
it.
However, recently I've been thinking: If the init() function has
to be called before any other function and should only be called once,
why not put the initialization call in a BEGIN block inside the module
itself? That way, the programmer who uses my module doesn't even have
to call the init() function; it will be done automatically at the "use
MyPackage;" statement.
My first question is: Is putting the initialization function
inside a BEGIN (or CHECK or INIT) block a good idea, or is there some
pontential problem that I'm not aware of? If the initialization
function fails, I want it to stop the program from running at all. Of
course, if I do put the init() function in a BEGIN block, I'll remove
all mention of it from the perldoc so the programmer won't be tempted
to use it.
And my second question: If it is a good idea to automatically call
the init() function for the programmer, should I:
a) put the init() statement in a BEGIN block
b) put the init() statement in a CHECK block
c) put the init() statement in an INIT block
d) put the init() statement as the last statement executed
in the module
or
e) it's not a good idea, so just let the programmer who uses
my module call it in his/her own code
The only difference I see between BEGIN, CHECK, and INIT (besides
the facts that BEGIN statments happen before CHECK statements which
happen before INIT statements and that CHECK statements happen in
"First In, Last Out" order) is that if I put the init() function in a
BEGIN or CHECK block and the init() function fails, then it is
discovered at compile time. In other words, if the init() function
fails, then the statement:
perl -c myscript.pl
will fail as well, as long as the init() function call is placed
inside a BEGIN block or a CHECK block.
This sounds appealing in that "use MyPackage;" will fail if the
init() function fails, bringing it right away to the attention of the
programmer. However, it's possible that I might not see an obvious
problem with this approach, and so I would like any advice or comments
from anyone who might have had experience with something similar.
To sum up my questions: Should I put the init() call in a BEGIN,
CHECK, or INIT block, or should I just let the programmer who uses my
module do it automatically (and document it thoroughly in the
perldoc)?
Thanks in advance for any advice.
-- Jean-Luc
I have a couple of questions and I would like to know the opinions
of the Perl community.
I while ago, I wrote a Perl module which is now practically
finished. Its exact contents aren't important for my questions,
except for the fact that it is a package and its functions are called
like:
use MyPackage;
MyPackage::init();
MyPackage::f1();
MyPackage::f2($var1, $var2);
The first function that must be called is an initialization
function (MyPackage::init()). It must be called before any other
function in the package and should only be called once. This fact is
well-documented in the perldoc documentation, so any Perl programmer
who bothers to check this module's documentation should be aware of
it.
However, recently I've been thinking: If the init() function has
to be called before any other function and should only be called once,
why not put the initialization call in a BEGIN block inside the module
itself? That way, the programmer who uses my module doesn't even have
to call the init() function; it will be done automatically at the "use
MyPackage;" statement.
My first question is: Is putting the initialization function
inside a BEGIN (or CHECK or INIT) block a good idea, or is there some
pontential problem that I'm not aware of? If the initialization
function fails, I want it to stop the program from running at all. Of
course, if I do put the init() function in a BEGIN block, I'll remove
all mention of it from the perldoc so the programmer won't be tempted
to use it.
And my second question: If it is a good idea to automatically call
the init() function for the programmer, should I:
a) put the init() statement in a BEGIN block
b) put the init() statement in a CHECK block
c) put the init() statement in an INIT block
d) put the init() statement as the last statement executed
in the module
or
e) it's not a good idea, so just let the programmer who uses
my module call it in his/her own code
The only difference I see between BEGIN, CHECK, and INIT (besides
the facts that BEGIN statments happen before CHECK statements which
happen before INIT statements and that CHECK statements happen in
"First In, Last Out" order) is that if I put the init() function in a
BEGIN or CHECK block and the init() function fails, then it is
discovered at compile time. In other words, if the init() function
fails, then the statement:
perl -c myscript.pl
will fail as well, as long as the init() function call is placed
inside a BEGIN block or a CHECK block.
This sounds appealing in that "use MyPackage;" will fail if the
init() function fails, bringing it right away to the attention of the
programmer. However, it's possible that I might not see an obvious
problem with this approach, and so I would like any advice or comments
from anyone who might have had experience with something similar.
To sum up my questions: Should I put the init() call in a BEGIN,
CHECK, or INIT block, or should I just let the programmer who uses my
module do it automatically (and document it thoroughly in the
perldoc)?
Thanks in advance for any advice.
-- Jean-Luc