当前位置:网站首页>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边栏推荐
- Esxi on arm 10/22 update
- How can fluke dsx2-5000 and dsx2-8000 modules find the calibration expiration date?
- PLC的选型
- 雷达成像 Matlab 仿真 4 —— 距离分辨率分析
- Photovoltaic power generation system MPPT maximum power point tracking
- MATLAB signal processing
- VAN(DWConv+DWDilationConv+PWConv)
- Trouble encountered in cable testing -- a case study of a manufacturer?
- VS Code 基础配置与美化
- clock tree分析实例
猜你喜欢

Weight decay

EXFO 730C光时域反射计只有iOLM光眼升级OTDR(开通otdr权限)

Pycharm2019 set editor theme and default code

(PHP graduation project) based on PHP student daily behavior management system access

Fluke fluke aircheck WiFi tester cannot configure file--- Ultimate solution experience

(PHP graduation project) based on PHP user online submission management system

set_false_path

Redhawk Dynamic Analysis

Low power design -power switch

ICC2(一)Preparing the Design
随机推荐
EMC experiment practical case ESD electrostatic experiment
Graduation thesis | how to write literature review
雷达成像 Matlab 仿真 1 —— LFM信号及其频谱
EfficientNET_ V1
PyTorch 学习笔记 3 —— DATASETS & DATALOADERS & TRANSFORMS
【YOLOv5】环境搭建:Win11 + mx450
ICC2(三)Clock Tree Synthesis
Pycharm2019设置编辑器主题和默认代码
T-sne dimension reduction visualization
Talking about fluke optical cable certification? What is CFP? What is OFP?
Hugging face 的入门使用
如何测试工业以太网线缆(利用FLUKE DSX-8000)?
RS232 RS485 RS422 communication learning and notes
How to pop up the message dialog box
Monitor the CPU temperature of raspberry pie 4B installed with esxi on ARM
set_ false_ path
(PHP graduation project) based on PHP student daily behavior management system access
Low power design isolation cell
Bag of Tricks训练卷积网络的技巧
Design and analysis of contactor coil control circuit