当前位置:网站首页>Introduction to Perl (IX) quotation
Introduction to Perl (IX) quotation
2022-07-28 06:28:00 【Shilu building】
A reference can be understood as a pointer , Play a directional role , Can point to scalar 、 Array 、 Hash tables and even sub functions .
One 、 Create reference
Reference is a scalar type , Prefix the referenced variable name with “\”, It means that the variable is referenced , Examples are as follows :
$scalarref = \$foo; # Scalar variable reference
$arrayref = \@ARGV; # References to arrays
$hashref = \%ENV; # Hash reference
$coderef = \&handler; # Sub function reference Multi dimensional arrays can be constructed by reference , Use here "[ ]" Define an anonymous array , If you refer to the array assignment method to use “()”, Then what you get is not a quote , It's the length of the returned array .
my $aref = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]Except for anonymous arrays , And anonymous hash table , Use “{ }” To define .
$href= { APR =>4, AUG =>8 };Sub function reference format : \&.
Call reference function format : & + Created reference name .
#!/usr/bin/perl
# Function definition
sub PrintHash{
my (%hash) = @_;
foreach $item (%hash){
print " Elements : $item\n";
}
}
%hash = ('name' => 'it', 'age' => 3);
# Create a reference to a function
$cref = \&PrintHash;
# Use references to call functions
&$cref(%hash);The operation results are as follows :
Elements : age
Elements : 3
Elements : name
Elements : itTwo 、 Dereference
Dereference can be used according to different types $, @ or % + Variable name to cancel , Scalar $, Array with @, Hash with %.
#!/usr/bin/perl
$var = 10; # $r quote $var Scalar
$r = \$var; # Output locally stored $r Variable value
print "$var by : ", $$r, "\n";
@var = (1, 2, 3); # $r quote @var Array
$r = \@var; # Output locally stored $r Variable value
print "@var by : ", @$r, "\n";
%var = ('key1' => 10, 'key2' => 20); # $r quote %var Hash
$r = \%var; # Output locally stored $r Variable value
print %var ," by : ", %$r, "\n";
sub add {
$count = @_[0] + @_[1];
}
$value = \&add;
print add(1,2), "=" &$value "\n"; It should be noted here that neither hash nor subfunction can be placed in double quotation marks ” “ in ,print You can use commas between strings of ”,“ Separate . The running result is :
10 by : 10
1 2 3 by : 123
key110key220 by : key110key220
3=3When you are not sure what type the reference is, you can use ref To judge .
#!/usr/bin/perl
$var = 10;
$r = \$var;
print "r The type of reference : ", ref($r), "\n";
@var = (1, 2, 3);
$r = \@var;
print "r The type of reference : ", ref($r), "\n";
%var = ('key1' => 10, 'key2' => 20);
$r = \%var;
print "r The type of reference : ", ref($r), "\n";The operation results are as follows :
r The type of reference : SCALAR
r The type of reference : ARRAY
r The type of reference : HASH边栏推荐
- ICC2(一)Preparing the Design
- 低功耗设计-Power Switch
- set_clock_groups
- T-sne dimension reduction visualization
- 浅谈误码仪的使用场景?
- EfficientNET_ V1
- Bert bidirectional encoder based on transformer
- npm yarn相关的操作
- AEM online product promotion conference - Cable certification tester
- For a large amount of data, matlab generates Excel files and typesetting processing source code
猜你喜欢

Overall understanding of PLC

set_case_analysis

ICC2分析时序的神器 analyze_design_violations

TCL and eltcl? Cdnext and CMRL?

Matlab simulation of radar imaging 3 - multi-target detection

How does fluke dtx-1800 test cat7 network cable?

Matlab simulation of radar imaging 4 - range resolution analysis

测量电脑电池容量

低功耗设计-Power Switch

CLIP Learning Transferable Visual Models From Natural Language Supervision
随机推荐
Perl入门学习(九)引用
DSX2-8000如何校准?校准流程?
EXFO 730C光时域反射计只有iOLM光眼升级OTDR(开通otdr权限)
Cronbach’s α? Kmo coefficient? Factor load? The most understandable course of questionnaire reliability and validity analysis in history!!! (SPSS and AMOS)
杭州某公司福禄克FLUKE DTX-SFM2单模模块-修复案例
Shuffle Net_ v1-shuffle_ v2
机器学习笔记 5 —— Logistic Regression
USB network native driver for esxi updated to support esxi7.0 Update 2
PyTorch 学习笔记 2 —— About Tensor
VAN(DWConv+DWDilationConv+PWConv)
Transformer 自注意力机制 及完整代码实现
Cautious speculation about fusion on Apple silicon
初学者进行传感器选型
毕业论文 | 文献综述应该怎么写
Selection of PLC
For a large amount of data, matlab generates Excel files and typesetting processing source code
set_ clock_ groups
AEM testpro K50 and south Guangdong survey
Overall understanding of PLC
npm yarn相关的操作