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


__END__


-- 
__END__

Just my 0.00000002 million dollars worth,
   --- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
  Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



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