Subroutines
-----------------
1. System and User Functions
1) The name of a subroutine is another Perl identifier (letters, digits, and underscores, but can't start with a digit) with a sometimes-optional ampersand (&) in front.
2) That subroutine name comes from a separate namespace, so Perl won't be confused if you have a subroutine called &fred and a scalar called $fred in the same program.
2. Defining a Subroutine
1) Subroutine definitions can be anywhere in your program text. In any case, you don't normally need any kind of forward declaration.
2) Subroutine definitions are global; without some powerful trickiness, there are no private subroutines. If you have two subroutine definitions with the same name, the later one overwrites the earlier one.
3. Return Values
1) The return value is the last expression evaluated in the body of a subroutine.
2) If the last expression of a subrountine is the print statement. Its return value will normally be 1, meaning "printing was successful,"[104] but that's not the return value we actually wanted. So be careful when adding additional code to a subroutine to ensure that the last expression evaluated will be the desired return value.
3) "The last expression evaluated" really means the last expression evaluated, rather than the last line of text. For example, this subroutine returns the larger value of $fred or $barney:
sub larger_of_fred_or_barney {
if ($fred > $barney) {
$fred;
} else {
$barney;
}
}
4. Arguments
1) The parameter list (another name for the argument list) is automatically assigned to a special array variable named @_ for the duration of the subroutine.
2) The @_ variable is local to the subroutine; if there's a global value in @_, it is saved away before the subroutine is invoked and restored to its previous value upon return from the subroutine. This also means that a subroutine can pass arguments to another subroutine without fear of losing its own @_ variable -- the nested subroutine invocation gets its own @_ in the same way. Even if the subroutine calls itself recursively, each invocation gets a new @_, so @_ is always the parameter list for the current subroutine invocation.
分享到:
相关推荐
Learning Perl teaches you the basics and shows you how to write programs up to 128 lines long—roughly the size of 90% of the Perl programs in use today. Each chapter includes exercises to help you ...
Learning Perl teaches you the basics and shows you how to write programs up to 128 lines long—roughly the size of 90% of the Perl programs in use today. Each chapter includes exercises to help you ...
Abaqus 用户单元子程序UEL Fortran程序开发
本文讲述如何在MDK(keil)中将1个子程序段定位在1个固定的地址位置。
This is the third in O'Reilly's series of landmark Perl tutorials, which started with "Learning Perl", the bestselling introduction that taught you the basics of Perl syntax, and "Intermediate Perl", ...
brian d foy is coauthor of Learning Perl, Fifth Edition (O’Reilly Media, 2008), and Intermediate Perl (O’Reilly Media, 2006), and author of Mastering Perl (O’Reilly Media, 2007). He established ...
writing subroutine with abaqus
Written for readers with at least some Perl programming experience, Mastering Algorithms in Perl delivers a solid library of algorithms written in Perl for business and mathematical computing....
BeginPerlBioinfo.pm module containing subroutines from the text You'll need the module BegPerlBioinfo.pm for most of the examples and exercises. Place it in the same directory (or folder) as the ...
ABAQUS自定义Subroutines程序编写指南,包含UMAT及UEL等
// Initialization Subroutines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Port_...
Use this subroutine to define any complex, constitutive models for materials that cannot be modeled with the available ABAQUS material models.
A fortran-77 computing procedure for a general solution of Lambert's problem has been developed, and its three main subroutines are listed. Details are given of the testing of this procedure. Much of...
ABAQUS umat
C++优化系列 - 2.Optimizing subroutines in assembly language An optimization guide for x86 platforms
Gain the skills to begin developing Perl 6 applications from the ground up in this hands-on compact book, which includes a foreword from Larry Wall, creator of Perl. You’ll learn enough to get ...
C SUBROUTINES WHICH MUST BE SUPPLIED BY THE USER: C C FORMOD(NP,ND,PM,DP,DM), COMPUTES THE FORWARD FUNCTION FOR MODEL PM() AT C THE DATA PARAMETERS DP() AND RETURNS IT IN DM(). C FORDIV(NP,ND,PM,DP,DM...
A library of DirectDraw graphics functions and subroutines. Being used in DDCK: MOC!
http://docs.rinet.ru:8080/Using_Perl5_in_Web/ (1 of 24) [2/17/2007 1:22:33 PM] Conditional Expressions H Compound Statements H Loops H Labels H Subroutines H Variable Scope H Patterns Regular ...