-----Original Message-----
From: Mr. Shawn H. Corey [mailto:[EMAIL PROTECTED] 
Sent: Monday, May 01, 2006 7:36 PM
To: chen li
Cc: beginners@perl.org
Subject: Re: What is the function of BEGIN in perl

On Mon, 2006-01-05 at 15:32 -0700, chen li wrote:
> Dear all,
> 
> Recently I read some source codes of perl modules. I
> find some codes  look like this:
> 
> BEGIN {..................} 
> 
> What is the function of BEGIN here?

>BEGIN clauses are executed immediately after they are compiled. As
such,
>they can be used to redirect the compilation of the remaining code. For
>example, if you write a module that is dependant on the operating
>system, that is, there is different code for different operating
>systems, you could use BEGIN to load the appropriate subroutines:

>#!/

>package MyModule;

>BEGIN {
>  if( $^O eq 'MSDos' ){
>    require 'MyModule::MSDos.pm';
>  }elsif( $^O eq 'MacOSX' ){
>    require 'MyModule::MacOSX.pm';
>  }else{
>    require 'MyModule::Generic.pm';
>  }
>}

>If the normal course of affairs, you would not need it. But if you do
>need it, it is there. (In other words, don't worry about it unless all
>else fails).
************************************************************************
***

Doesn't the BEGIN and INIT constructs provide the same behavior?

_________________________________________________

This message is for the designated recipient only and may contain privileged, 
proprietary, or otherwise private information. If you have received it in 
error, please notify the sender immediately and delete the original. Any other 
use of the email by you is prohibited.

Dansk - Deutsch - Espanol - Francais - Italiano - Japanese - Nederlands - Norsk 
- Portuguese - Svenska: www.cardinalhealth.com/legal/email

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to