RE: What is the function of BEGIN in perl

2006-07-05 Thread Smith, Derek


-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




Re: What is the function of BEGIN in perl

2006-07-05 Thread Mr. Shawn H. Corey
Smith, Derek wrote:
 Doesn't the BEGIN and INIT constructs provide the same behavior?

No. From `perldoc perlrun`:

   -c   causes Perl to check the syntax of the program and then exit
without executing it.  Actually, it will execute BEGIN, CHECK, and
use blocks, because these are considered as occurring outside the
execution of your program.  INIT and END blocks, however, will be
skipped.



-- 
__END__

Just my 0.0002 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




RE: What is the function of BEGIN in perl

2006-07-05 Thread Smith, Derek


-Original Message-
From: Mr. Shawn H. Corey [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, July 05, 2006 1:21 PM
To: Perl Beginners
Subject: Re: What is the function of BEGIN in perl

Smith, Derek wrote:
 Doesn't the BEGIN and INIT constructs provide the same behavior?

No. From `perldoc perlrun`:

   -c   causes Perl to check the syntax of the program and then exit
without executing it.  Actually, it will execute BEGIN, CHECK, and
use blocks, because these are considered as occurring outside the
execution of your program.  INIT and END blocks, however, will be
skipped.



-- 
__END__

Just my 0.0002 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/

**

Ok so what is the diff then?
I search through all my emails, programming perl, perl best practices
and PerlMonks but did not find anything.

Derek Bellner Smith
Unix Systems Engineer
Cardinal Health Dublin, Ohio


_

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




Re: What is the function of BEGIN in perl

2006-05-02 Thread chen li
Thanks very much for Anthiny and others for the
explanation.

Li

--- Anthony Ettinger [EMAIL PROTECTED] wrote:

 basically, BEGIN { } is a block which gets called
 whenever the script
 begins.
 
 
 
 On 5/1/06, chen li [EMAIL PROTECTED] 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?
 
  Thank you,
 
  Li
 
  __
  Do You Yahoo!?
  Tired of spam?  Yahoo! Mail has the best spam
 protection around
  http://mail.yahoo.com
 
  --
  To unsubscribe, e-mail:
 [EMAIL PROTECTED]
  For additional commands, e-mail:
 [EMAIL PROTECTED]
  http://learn.perl.org/
 http://learn.perl.org/first-response
 
 
 
 
 
 --
 Anthony Ettinger
 Signature: http://chovy.dyndns.org/hcard.html
 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




What is the function of BEGIN in perl

2006-05-01 Thread chen li
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?

Thank you,

Li

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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




RE: What is the function of BEGIN in perl

2006-05-01 Thread Charles K. Clarkson
chen li wrote:

: Recently I read some source codes of perl modules. I
: find some codes  look like this:
: 
: BEGIN {..}
: 
: What is the function of BEGIN here?

Read BEGIN, CHECK, INIT and END in the perlmod file.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328



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




Re: What is the function of BEGIN in perl

2006-05-01 Thread Anthony Ettinger

basically, BEGIN { } is a block which gets called whenever the script
begins.



On 5/1/06, chen li [EMAIL PROTECTED] 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?

Thank you,

Li

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

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






--
Anthony Ettinger
Signature: http://chovy.dyndns.org/hcard.html


Re: What is the function of BEGIN in perl

2006-05-01 Thread Mr. Shawn H. Corey
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.0002 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