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.
分享到:
相关推荐
五、子程序(Subroutines) Perl中的函数定义使用`sub`关键字,可以接受参数并返回值: ```perl sub add { my ($a, $b) = @_; return $a + $b; } print add(2, 3); # 输出5 ``` 六、块和标号 1. 块:以`{}`...
根据提供的文件信息“perl-pocket-ref-5.004.1.pdf”,这是一份关于Perl 5.004版本的快速参考指南。该文档详细介绍了Perl编程语言的基础语法、变量、运算符、函数等内容,并提供了针对不同应用场景的实用模块介绍。...
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 ...
Perl是一种强大的脚本编程语言,尤其在文本处理和系统管理任务方面表现突出。要了解如何使用Perl以及它的基本用法,我们首先需要安装Perl解释器,通常可以在官方网站`www.perl.org`上找到对应操作系统的版本。安装...
**《Learning Perl Student Workbook》(第二版,2012.01)** 这本书是由Brian d'foy编写的,旨在帮助初学者通过一系列练习来掌握Perl编程语言的基础知识。作为一本学生工作手册,它包含了丰富的实践性练习和示例...
《Learning Perl (6th Edition)》是针对初学者编写的一本Perl编程语言教程,它详细介绍了Perl语言的基础知识和应用。本书由Randal L. Schwartz、briandfoy和Tom Phoenix三人合著,是Perl语言的经典入门书籍之一。 ...
《Abaqus用户单元子程序UEL Fortran程序开发详解》 在有限元分析领域,Abaqus是一款广泛应用的软件,其强大的功能和灵活性使得它在解决复杂工程问题时独树一帜。尤其对于特殊或者非标准的问题,Abaqus提供了用户...
在嵌入式开发中,有时候我们需要确保某些代码或数据在内存中的特定位置,例如中断服务例程、固件更新区域等。MDK(Microcontroller Development Kit,也称为Keil uVision)是ARM微控制器常用的集成开发环境,它支持C...
- 通过XS(X Subroutines)机制可以直接从Perl调用C函数。这有助于利用C语言编写性能关键部分,同时保持Perl的灵活性。 以上是对给定文件中的知识点进行了详细解读与总结。希望这些信息能够帮助读者更好地理解Perl...
首先,我们要理解用户子程序(User-Defined Subroutines)的概念。在ABAQUS中,用户可以通过编写Fortran代码来实现特定的物理过程或复杂的工程行为。这些子程序包括UMAT(用于定义材料行为)、UHARM(处理非线性谐波...
4. **Subroutines**:以波浪线`&`开头,表示函数。 Perl也支持面向对象编程和用户自定义类型。 #### Scalars 数字和字符串是两种基本的标量类型: ```perl $num = 100; # 数字 $str = 'unix tools'; # 字符串 $...
此外,还有子程序(subroutines),它们是可重用的代码块,有助于提高代码的模块化和可读性。 Perl的正则表达式(Regular Expressions)是其的一大亮点,用于高效地进行字符串匹配和替换。这个教程会详细讲解正则...
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 ...
// Initialization Subroutines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Port_...
ABAQUS是一款广泛应用于工程模拟领域的有限元分析软件,它提供了强大的用户自定义子程序功能,即User Subroutines,允许用户通过编写自己的代码来扩展ABAQUS的计算能力。这些子程序主要包括UMAT(材料子程序)、UEL...
在编程领域,特别是嵌入式系统和复古计算机领域,Z80处理器因其高效性和灵活性而备受推崇。...通过深入研究"Z80-Subroutines-main"这样的资源,开发者可以更好地理解如何在实际项目中有效地利用这种技术。
- **第11章:Subroutines and variable scoping**(子程序与变量作用域) - 介绍Perl中的子程序定义与调用。 - 探讨局部变量与全局变量的区别以及最佳实践。 - **第12章:Modules and the CPAN**(模块与CPAN) ...
Use this subroutine to define any complex, constitutive models for materials that cannot be modeled with the available ABAQUS material models.