当前位置:网站首页>Perl introductory learning (VIII) subroutine
Perl introductory learning (VIII) subroutine
2022-07-28 06:28:00 【Shilu building】
One 、 Definition
A subfunction is a piece of separate code defined by the user in the program , It can appear anywhere in the program , Its function is to avoid using the same piece of code multiple times , The writing is complicated , Setting it as a sub function can effectively reduce repeated code , Make the program easy to read .
The definition method is :
sub subroutine
{
statements;
}Two 、 call
Subroutines can appear anywhere , In writing order, you can call first and then define , You can also define it first and then call , For reading experience , When calling first and then defining, you can define the subroutine name first , Examples are as follows :
## Call first and then define
subroutine( parameter list );
sub subroutine {
body
}
OR
sub subroutine;
subroutine( parameter list );
sub subroutine {
body
}
## First define and then invoke
sub subroutine {
body
}
subroutine( parameter list );Subroutines can call each other , It is called recursive subroutine , Local variables should be used except for variables that will not be overwritten by subroutines (my,local) , in addition , You need code that can stop recursive calls .
3、 ... and 、 Return value
Subroutines can use return Statement to get the return value , without return sentence , The default return value is the value of the last line of statement . Examples are as follows :
#!/usr/bin/perl
sub add {
return 1 + 2;
1+1;
}
print add();The return value is 3, Because there is return.
Four 、 local variable
By default , All variables are global variables , It can be called anywhere in the program , If you want to create a local variable , have access to my perhaps local The operator ,my The function of is limited to the current subroutine ,local In addition to applying to the current subroutine , It is still useful in the subroutine called by this subroutine , Simple memory is :my、local、 The variable names of global variables are the same :
1、 The priority in the subroutine is local> Global variables >my. here local It can be understood as the temporary assignment of global variables ,
2、 Outside the subroutine, only global variables work .
Examples are as follows :
#!/usr/bin/perl
$x = 1;
sub sub1 {
print "current $x\n";
}
sub sub2 {
my $x = 2;
print "my $x\n"
sub1;
}
sub sub3 {
local $x = 3;
print "local $x\n"
sub1;
}
sub2;
sub3;
print "$x\n"The running result is :
my 2
current 1 # my Only the current subroutine
local 3
current 3 # local Can act on sub3 Subroutine called sub1 in .
1 # Outside the subroutine , Only global variables work When sub2 Run time output my $x Is a local variable , When sub2 Call in sub1 when , local variable my $x Cannot be called , perform sub1 At the time of the $x The global variable definition value is 1.sub3 Run time output local $ Is a local variable , Act on the current subroutine , When sub3 Call in sub1 when ,local The defined variables apply to sub1, So execute sub1 when $x Time local variable local $x Value 3.
5、 ... and 、 Pass parameters to subroutines
PERL The parameter list passed to the subroutine is automatically stored in the array @_ in , Therefore, the first parameter of the subroutine is $_[0], The second parameter is $_[1], And so on . What needs to be noted here is , When the objects passed are scalars and arrays , You need to put the array behind .
Pass scalars and arrays :
#!/usr/bin/perl
# Defined function
sub PrintList{
my @list = @_;
print " The list is : @list\n";
}
$a = 10;
@b = (1, 2, 3, 4);
# List parameters
PrintList($a, @b);The running result is :
10 1 2 3 4Pass hash :
#!/usr/bin/perl
# Method definition
sub PrintHash{
my (%hash) = @_;
foreach my $key ( keys %hash ){
my $value = $hash{$key};
print "$key : $value\n";
}
}
%hash = ('name' => 'it', 'age' => 3);
# Pass hash
PrintHash(%hash);The running result is :
age : 3
name : it边栏推荐
- DSX2-8000如何校准?校准流程?
- Bag of Tricks训练卷积网络的技巧
- Triode design, understanding saturation, linear region and cut-off region
- set_multicycle_path
- How to view the transfer function of the module directly built by Simulink
- For a large amount of data, matlab generates Excel files and typesetting processing source code
- What about the insufficient memory of Clickhouse aggregation? Then improve the polymerization performance
- 低功耗设计-Power Switch
- Communication between DSP and FPGA
- Varistor design parameters and classic circuit recording hardware learning notes 5
猜你喜欢

Terminal resistance detailed signal complete series hardware learning notes 7

Transformer 自注意力机制 及完整代码实现

低功耗设计-isolation cell

Pycharm2019 set editor theme and default code

set_false_path

初学者进行传感器选型

VS Code 基础配置与美化

A NOVEL DEEP PARALLEL TIME-SERIES RELATION NETWORK FOR FAULT DIAGNOSIS

Design and analysis of contactor coil control circuit

t-SNE降维可视化
随机推荐
PyTorch 学习笔记 1 —— Quick Start
低功耗设计-isolation cell
set_multicycle_path
Arduino reads the analog voltage_ How mq2 gas / smoke sensor works and its interface with Arduino
PyTorch 学习笔记 3 —— DATASETS & DATALOADERS & TRANSFORMS
What is the AEM testpro cv100 and fluke dsx-8000 of category 8 network cable tester?
clock tree分析实例
Photovoltaic power generation system MPPT maximum power point tracking
福禄克DSX2-5000、DSX2-8000模块如何找到校准到期日期?
雷达成像 Matlab 仿真 1 —— LFM信号及其频谱
Efficient Net_ V2
线缆测试中遇到苦恼---某厂商案例分析?
Exploration of Clickhouse aggregation internal mechanism of aggregation
雷达成像 Matlab 仿真 3 —— 多目标检测
Fluke dtx-sfm2 single mode module of a company in Hangzhou - repair case
set_false_path
低功耗设计-Power Switch
Matlab simulation of radar imaging 1 - LFM signal and its spectrum
ASP. Net read database bound to treeview recursive mode
WebService error maximum message size quota for incoming messages (65536) has been exceeded