Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > BEGIN, INIT etc...

Reply
Thread Tools

BEGIN, INIT etc...

 
 
pgodfrin
Guest
Posts: n/a
 
      03-26-2008
Greetings,
Anyone know of a good article that discusses when and why you would
use the BEGIN, UNITCHECK, CHECK, INIT and END code blocks?

thanks,
pg
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-26-2008
pgodfrin wrote:
> Anyone know of a good article that discusses when and why you would
> use the BEGIN, UNITCHECK, CHECK, INIT and END code blocks?


perldoc perlmod

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
pgodfrin
Guest
Posts: n/a
 
      03-26-2008
On Mar 26, 3:49 pm, Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
> pgodfrin wrote:
> > Anyone know of a good article that discusses when and why you would
> > use the BEGIN, UNITCHECK, CHECK, INIT and END code blocks?

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


been there done that. Can we do better?
pg
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-26-2008
pgodfrin <(E-Mail Removed)> wrote:
> On Mar 26, 3:49 pm, Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
> > pgodfrin wrote:
> > > Anyone know of a good article that discusses when and why you would
> > > use the BEGIN, UNITCHECK, CHECK, INIT and END code blocks?

> >
> > perldoc perlmod
> >

>
> been there done that. Can we do better?


What parts do you think need improving?

After reading my installed perlmod, I know what BEGIN and END do, and I
know that if I want to do what they do, then I should use the one that does
what I want done. I don't know what more to ask of documentation than
that. For CHECK and INIT, I know that they are used for things I will
probably rarely if ever need to do, but they are in the back of mind,
reminding me to re-read the docs if I ever do decide I might need them.
Again, It isn't clear to me what more is needed.

After reading the 5.10 perlmod, I see that UNITCHECK should be used if
I need something like CHECK but which is run in string evals (and some
other special constructs) unlike CHECK. Maybe it should point out that
UNITCHECK is newer and perhaps existing CHECK and INIT code would be better
done in the newer style as UNITCHECK code.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
Reply With Quote
 
pgodfrin
Guest
Posts: n/a
 
      03-27-2008
On Mar 26, 5:37 pm, (E-Mail Removed) wrote:
> pgodfrin <(E-Mail Removed)> wrote:
> > On Mar 26, 3:49 pm, Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
> > > pgodfrin wrote:
> > > > Anyone know of a good article that discusses when and why you would
> > > > use the BEGIN, UNITCHECK, CHECK, INIT and END code blocks?

>
> > > perldoc perlmod

>
> > been there done that. Can we do better?

>
> What parts do you think need improving?
>
> After reading my installed perlmod, I know what BEGIN and END do, and I
> know that if I want to do what they do, then I should use the one that does
> what I want done. I don't know what more to ask of documentation than
> that. For CHECK and INIT, I know that they are used for things I will
> probably rarely if ever need to do, but they are in the back of mind,
> reminding me to re-read the docs if I ever do decide I might need them.
> Again, It isn't clear to me what more is needed.
>
> After reading the 5.10 perlmod, I see that UNITCHECK should be used if
> I need something like CHECK but which is run in string evals (and some
> other special constructs) unlike CHECK. Maybe it should point out that
> UNITCHECK is newer and perhaps existing CHECK and INIT code would be better
> done in the newer style as UNITCHECK code.
>
> Xho
>
> --
> --------------------http://NewsReader.Com/--------------------
> The costs of publication of this article were defrayed in part by the
> payment of page charges. This article must therefore be hereby marked
> advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
> this fact.


Hi Xho,
Sorry - didn't mean to impugn the quality of the docs. All I was
looking for is some sort of article from a trade rag, for instance,
that discussed the utility of those code blocks. You know, maybe some
sort of article where a really good Perl programmer describes how he
(or she ) uses those code blocks in an effective manner.

That's all.

smiles,
pg
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      03-27-2008
>>>>> "p" == pgodfrin <(E-Mail Removed)> writes:

p> On Mar 26, 5:37 pm, (E-Mail Removed) wrote:
>> pgodfrin <(E-Mail Removed)> wrote:
>> > On Mar 26, 3:49 pm, Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
>> > > pgodfrin wrote:
>> > > > Anyone know of a good article that discusses when and why you would
>> > > > use the BEGIN, UNITCHECK, CHECK, INIT and END code blocks?

>>

p> Sorry - didn't mean to impugn the quality of the docs. All I was
p> looking for is some sort of article from a trade rag, for instance,
p> that discussed the utility of those code blocks. You know, maybe some
p> sort of article where a really good Perl programmer describes how he
p> (or she ) uses those code blocks in an effective manner.

simple answer is to ignore INIT, CHECK and the new UNITCHECK. they are
only for specialized modules and newbies should never need to go near
them. they fall into the rule (which covers symrefs and string eval)
that you shouldn't use them until you know when you don't need them.

BEGIN and END are much more useful and you seem to know about them.

they are commonly used when you need to do something as soon as that
block of code is parsed (BEGIN) or when the program is exiting
(END). and the above rule applies as you don't know when you need or not
need them so don't bother to learn them yet. once you get into perl
deeper you will know more about initialization, compile vs runtime and
destruction. but now you don't know those so ignore BEGIN/END until you
need to learn them.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
pgodfrin
Guest
Posts: n/a
 
      03-27-2008
On Mar 27, 11:31 am, (E-Mail Removed) wrote:
> On Thu, 27 Mar 2008 03:26:31 GMT, Uri Guttman <(E-Mail Removed)>
> wrote:
>
>
>
> >>>>>> "p" == pgodfrin <(E-Mail Removed)> writes:

>
> > p> On Mar 26, 5:37 pm, (E-Mail Removed) wrote:
> > >> pgodfrin <(E-Mail Removed)> wrote:
> > >> > On Mar 26, 3:49 pm, Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
> > >> > > pgodfrin wrote:
> > >> > > > Anyone know of a good article that discusses when and why you would
> > >> > > > use the BEGIN, UNITCHECK, CHECK, INIT and END code blocks?

>
> > p> Sorry - didn't mean to impugn the quality of the docs. All I was
> > p> looking for is some sort of article from a trade rag, for instance,
> > p> that discussed the utility of those code blocks. You know, maybe some
> > p> sort of article where a really good Perl programmer describes how he
> > p> (or she ) uses those code blocks in an effective manner.

>
> >simple answer is to ignore INIT, CHECK and the new UNITCHECK. they are
> >only for specialized modules and newbies should never need to go near
> >them. they fall into the rule (which covers symrefs and string eval)
> >that you shouldn't use them until you know when you don't need them.

>
> >BEGIN and END are much more useful and you seem to know about them.

>
> >they are commonly used when you need to do something as soon as that
> >block of code is parsed (BEGIN) or when the program is exiting
> >(END). and the above rule applies as you don't know when you need or not
> >need them so don't bother to learn them yet. once you get into perl
> >deeper you will know more about initialization, compile vs runtime and
> >destruction. but now you don't know those so ignore BEGIN/END until you
> >need to learn them.

>
> >uri

>
> You don't need to know it until you know it? How does that work?
> Something like not learning how to drive on ice until after a fatal
> skid?
>
> I also would like to learn this topic in advance of need.


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?

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?

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

thanks,
pg
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      03-27-2008
>>>>> "d" == dummy <(E-Mail Removed)> writes:

d> On Thu, 27 Mar 2008 03:26:31 GMT, Uri Guttman <(E-Mail Removed)>
d> wrote:

>> they are commonly used when you need to do something as soon as that
>> block of code is parsed (BEGIN) or when the program is exiting
>> (END). and the above rule applies as you don't know when you need or not
>> need them so don't bother to learn them yet. once you get into perl
>> deeper you will know more about initialization, compile vs runtime and
>> destruction. but now you don't know those so ignore BEGIN/END until you
>> need to learn them.
>>
>> uri


d> You don't need to know it until you know it? How does that work?
d> Something like not learning how to drive on ice until after a fatal
d> skid?

no, you learn to drive on ice only after you have mastered general
driving and have a need or desire to learn more. most newbies have no
need for BEGIN/END and so don't need to know about them. as they learn
more perl and get to know modules, scoping and compile vs runtime then
those concepts will help them learn how and when to use BEGIN/END. my
point is that they are not needed by newbies and shouldn't be taught as
they will just confuse them.

d> I also would like to learn this topic in advance of need.

do you write modules? do you have subtle initialization or destruction
needs? if not, you don't need to learn them yet. read perlmod more times
and if you don't grasp the concepts then get a good perl book. i can
explain them but it would require a decent sized post or small
article. it makes a good topic and i may do it one day. i already have
seen a need for a definitive article on lists vs arrays and i have been
writing it in my head. but too many things are higher priority for me
now to do that.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      03-27-2008
pgodfrin <(E-Mail Removed)> writes:

> 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/
 
Reply With Quote
 
pgodfrin
Guest
Posts: n/a
 
      03-27-2008
On Mar 27, 12:00 pm, Uri Guttman <(E-Mail Removed)> wrote:
> >>>>> "d" == dummy <(E-Mail Removed)> writes:

>
> d> On Thu, 27 Mar 2008 03:26:31 GMT, Uri Guttman <(E-Mail Removed)>
>
> d> wrote:
>
> >> they are commonly used when you need to do something as soon as that
> >> block of code is parsed (BEGIN) or when the program is exiting
> >> (END). and the above rule applies as you don't know when you need or not
> >> need them so don't bother to learn them yet. once you get into perl
> >> deeper you will know more about initialization, compile vs runtime and
> >> destruction. but now you don't know those so ignore BEGIN/END until you
> >> need to learn them.
> >>
> >> uri

>
> d> You don't need to know it until you know it? How does that work?
> d> Something like not learning how to drive on ice until after a fatal
> d> skid?
>
> no, you learn to drive on ice only after you have mastered general
> driving and have a need or desire to learn more. most newbies have no
> need for BEGIN/END and so don't need to know about them. as they learn
> more perl and get to know modules, scoping and compile vs runtime then
> those concepts will help them learn how and when to use BEGIN/END. my
> point is that they are not needed by newbies and shouldn't be taught as
> they will just confuse them.
>
> d> I also would like to learn this topic in advance of need.
>
> do you write modules? do you have subtle initialization or destruction
> needs? if not, you don't need to learn them yet. read perlmod more times
> and if you don't grasp the concepts then get a good perl book. i can
> explain them but it would require a decent sized post or small
> article. it makes a good topic and i may do it one day. i already have
> seen a need for a definitive article on lists vs arrays and i have been
> writing it in my head. but too many things are higher priority for me
> now to do that.
>
> uri
>
> --
> Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com--
> ----- Perl Code Review , Architecture, Development, Training, Support ------
> --------- Free Perl Training ---http://perlhunter.com/college.html---------
> --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com---------


cool - please let us know when you've written these articles. Where do
those articles get published, btw?
pg
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
init of class members : mem(0) vs. mem() vs. not-init at all news.aon.at C++ 11 01-29-2011 07:30 PM
questions about object initialization, default-init and value-init Jess C++ 4 05-04-2007 02:47 AM
Sequence Order between Page Init and User Control Init Tony Cheng ASP .Net 1 02-24-2006 01:56 PM
Compiler/Linker Error undefined reference to 'std::ios_base::Init::Init[in-charge]() clusardi2k@aol.com C++ 1 08-18-2005 07:11 PM
init RAM with .rif bhb VHDL 1 01-30-2004 04:14 PM



Advertisments