in reply to Re^2: sub BEGIN
in thread sub BEGIN

It seems likely (functionally) unnecessary in this specific case. But some prefer not to analyze each specific case to try to convince themselves that it is actually foolproofly unnecessary and so just include BEGIN for such initializations as a "best practice". (The END block is likely just a visual reminder for "this is where you put clean-up code" and "I didn't forget the clean-up code, we just don't need any for this module at this time" -- as such, I might comment it out for the sake of clarity.)

The flow of the above code w/o the BEGIN goes like:

Compile: package iControlTypeCast; Compile: require SOAP::Lite; # nothing happens Compile: my $urnMap; # declares the variable Compile: $urnMap = ...; # nothing happens Compile: sub SOAP::Deserializer::typecast # makes it possible to cal +l this Run: require SOAP::Lite; # compiles and then runs S +OAP/Lite.pm # During the loading of SOAP::Lite, a whole ton # of stuff can happen and is likely to happen Run: my $urnMap = ...; # the variable is /finally +/ initialized

In this particular case, it seems unlikely that anything that happens during the loading of SOAP::Lite would lead to SOAP::Deserializer::typecast() being called. But I'd actually have to study the SOAP modules quite a bit more before I was fully convinced of that. It would certainly be bad style for that order of execution, IMHO. But I see no reason to just rely on such an order of execution never arising.

So it seems wise to avoid the window of time where SOAP::Deserializer::typecast() can be called while $urnMap is still not initialized. The BEGIN block avoids exactly that. So, I consider it good style/practice.

- tye        

Replies are listed 'Best First'.
Re^4: sub BEGIN (wise)
by Anonymous Monk on May 10, 2010 at 20:48 UTC
    hey thanks for the explanation. It looks like i have to study soap::lite more(as well as i can understand that is)
    All in all, from my understanding reading this module, only purpose of it is that to make sure key(e.g, {urn:iControl}System.ConfigSync.SaveMode) that user is inputting is in the list of urnMap before allowing it to use.. Am I correct on this?