当前位置:网站首页>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边栏推荐
- 初学者进行传感器选型
- Detailed explanation of word mail merge function: after merging, multiple word documents are generated and blank pages are deleted
- Learning notes on hardware circuit design 2 -- step-down power circuit
- mysql join技巧
- set_multicycle_path
- Low power design isolation cell
- ClickHouse 中的公共表表达式CTE
- 低功耗设计-Power Switch
- How to test industrial Ethernet cables (using fluke dsx-8000)?
- t-SNE降维可视化
猜你喜欢

Low power design isolation cell

Efficient Net_V2

ConNeXt

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

Create a basic report using MS chart controls

ICC2(三)Clock Tree Synthesis

Arduino reads the analog voltage_ How mq2 gas / smoke sensor works and its interface with Arduino

浅谈FLUKE光缆认证?何为CFP?何为OFP?

雷达成像 Matlab 仿真 2 —— 脉冲压缩与加窗

Synopsys Multivoltage Flow
随机推荐
clickhouse建宽表多少列最合适?
ClickHouse 中的公共表表达式CTE
当mysql表从压缩表变成普通表会发生什么
The short jumper dsx-8000 test is normal, but the dsx-5000 test has no length display?
clickhouse聚合之探索聚合内部机制
ICC2(四)Routing and Postroute Optimization
mysql删表不删库
PyTorch 学习笔记 4 —— 自动计算梯度下降 AUTOGRAD
福禄克DTX-1800其配件DTX-CHA002通道适配器CHANNEL更换RJ45插座小记
Chinese display problem of calendarextender control
PyTorch 学习笔记
Fluke dtx-sfm2 single mode module of a company in Hangzhou - repair case
mysql join技巧
set_ case_ analysis
Low power design isolation cell
EfficientNET_V1
T-sne dimension reduction visualization
USB network native driver for esxi updated to support esxi7.0 Update 2
set_false_path
Efficient Net_ V2