perl-basic-数据类型&引用
我觉得这一系列的标题应该是:PERL,从入门到放弃
USE IT OR U WILL LOSE IT
参考资料:
https://qntm.org/files/perl/perl.html
在线perl编译器:
https://www.tutorialspoint.com/execute_perl_online.php
use strict;
use warnings;
# no block comments # variables: use 'my' for declaration
# 1. scalars: undef(like NULL), number, string, reference to any other variables
# no distinguish between float and int
my $num = 12.34;
# use '.' for string concatenation
my $str = "hello, "."m'lady";
print $str."\n";
# no boolean type. but "false" in condition means: 0,"","0",undef
# 2. arrays: use @variableName
# trailing comma is OK
my @arr = (1, "hi", 3,);
# use '$' because it's scala when retrived
print $arr[0]."\n";
# negative index means retriving from backend
print "$arr[-2]\n";
# '' will not interpret, just output raw string
# "" will interpret variables
# they're same: print 'Hello \$str \@arr'
print "Hello \$str \@arr" # OPERATORS
# Numerical operators: <, >, <=, >=, ==, !=, <=>, +, *
# String operators: lt, gt, le, ge, eq, ne, cmp, ., x
啦啦啦今天继续啊
数组长度:
my @arr = (2, "Hi", "my", "lady", "!");
# the length of array
print scalar @arr;
print "\n";
# the end index of array. aka, length-1
print $#arr;
- hash Tables:
use strict;
use warnings; # hash variable
my %hashTable = ("1st" => "hi",
"2nd" => "my",
"3rd" => "lady");
# since some element is a scalar
print $hashTable{"1st"};
use strict;
use warnings; my %hashTable = (1=>"hi", 2=>"my", "3rd"=>"lady"); # convert to array
my @arr = %hashTable;
# 1hi2my3rdlady
print @arr;
# output 1
print $arr[0]; # 索引时hash用{},array[],""有无无所谓;构建时两者都用()
#print $hashTable{1};
#print $hashTable{"1"};
- list
列表指的是用()构建的东西,array和hash都是。列表可以作为一个元素放在列表中间,但是会失去层次结构。hash的话,

use strict;
use warnings; my @array = (
"apples",
"bananas",
(
"inner",
"list",
"several",
"entries",
),
"cherries",
);
my $i = 0;
while ($i <= $#array) {
print "array[$i] is ".$array[$i]."\n";
$i = $i + 1;
}

但其实list与array还是不同的,如下例:
my @arr = ("alpha", "beta", "gamma", "pie");
# 这里得到arr的长度
my $num = @arr;
print "$num\n";
# output: pie $num1存的是最后一项
my $num1 = ("alpha", "beta", "gamma", "pie");
print "$num1\n";
Every expression in Perl is evaluated either in scalar context or list context
而对很多函数来说,对scalar和list的处理方式是不同的,如reverse:
# link. no reverse
print reverse "hello, my lady", 3, " ";
print "\n";
# reverse each letter
my $scalar = reverse "hello, my lady";
print "$scalar\n";

可以在前面强制加scalar,让函数可以按照scalar的方式处理:
print scalar reverse "hello world"; # "dlrow olleh"
而在下面的例子中,给$outer[3]赋值时,由于它是scalar,所以取得的值只能是数组长度,而非数组。
my @outer = ("Sun", "Mercury", "Venus", undef, "Mars");
my @inner = ("Earth", "Moon");
$outer[3] = @inner;
print $outer[3]; # "2"
好吧,为了解决这个问题,使用引用:
my $colour = "Indigo";
# add reference
my $scalarRef = \$colour;
print $colour; # "Indigo"
print $scalarRef; # e.g. "SCALAR(0x182c180)"
print ${ $scalarRef }; # "Indigo"
# 如果不会混淆,可以省略{}
print $$scalarRef; my @colours = ("Red", "Orange", "Yellow", "Green", "Blue");
my $arrayRef = \@colours; print $colours[0]; # direct array access
print ${ $arrayRef }[0]; # use the reference to get to the array
print $arrayRef->[0]; # exactly the same thing my %atomicWeights = ("Hydrogen" => 1.008, "Helium" => 4.003, "Manganese" => 54.94);
my $hashRef = \%atomicWeights; print $atomicWeights{"Helium"}; # direct hash access
print ${ $hashRef }{"Helium"}; # use a reference to get to the hash
print $hashRef->{"Helium"}; # exactly the same thing - this is very common
我们希望定义自己的数据类型,类似C++里类的概念,所以我们这样做:
# Braces denote an anonymous hash
my $owner1Ref = {
"name" => "Santa Claus",
"DOB" => "1882-12-25",
}; my $owner2Ref = {
"name" => "Mickey Mouse",
"DOB" => "1928-11-18",
}; # Square brackets denote an anonymous array
my $ownersRef = [ $owner1Ref, $owner2Ref ]; my %account = (
"number" => "12345678",
"opened" => "2000-01-01",
"owners" => $ownersRef,
);
注意,这里$owner1Ref和$owner2Ref存的其实是一个匿名hash的引用,$ownersRef存的则是匿名array的引用。既然时候引用,
它们的实际值就是地址。所以%account中才能够保存完整的信息。等价于:
my %account = (
"number" => "31415926",
"opened" => "3000-01-01",
"owners" => [
{
"name" => "Philip Fry",
"DOB" => "1974-08-06",
},
{
"name" => "Hubert Farnsworth",
"DOB" => "2841-04-09",
},
],
);
如何打印?使用->
print "Account #", $account{"number"}, "\n";
print "Opened on ", $account{"opened"}, "\n";
print "Joint owners:\n";
print "\t", $account{"owners"}->[0]->{"name"}, " (born ", $account{"owners"}->[0]->{"DOB"}, ")\n";
print "\t", $account{"owners"}->[1]->{"name"}, " (born ", $account{"owners"}->[1]->{"DOB"}, ")\n";
perl-basic-数据类型&引用的更多相关文章
- Delphi、C C++、Visual Basic数据类型的对照 转
Delphi.C C++.Visual Basic数据类型的对照 变量类型 Delphi C/C++ Visual Basic 位有符号整数 ShortInt char -- 位无符号整数 Byte ...
- [Perl系列—] 2. Perl 中的引用使用方法
Perl 中的引用,为什么要使用引用? 对于熟悉C语言的开发人员来说, 指针这个概念一定不陌生. Perl 的引用就是指针,能够指向变量.数组.哈希表甚至子程序. Perl5中的两种Perl引用类型为 ...
- perl 对象 bless 引用
[root@dr-mysql01 ~]# cat aa.pl use LWP::UserAgent; use Data::Dumper; my $ua = LWP::UserAgent->new ...
- perl 函数回调 引用$client->run(sub {$client->sync});
匿名函数引用: [root@wx03 wx]# perl a1.pl CODE(0x2077b30) test [root@wx03 wx]# cat a1.pl $ref= sub {return ...
- 【转载】Perl中的引用
为什么使用引用? 在perl4中,hash表中的value字段只能是scalar,而不能是list,这对于有些情况是很不方便的,比如有下面的数据: Chicago, USAFrankfurt, Ger ...
- perl 中的引用
perl 语言中的引用共分为两类: 声明引用时只需要在对象的前面加上反斜杠 第一列是数组的引用: 代码示例: , , ); my $array_ref = \@array; 第二种是哈希的引用 代码示 ...
- perl的匿名引用
perl中列表不能嵌套,只能通过引用的方式构建复杂的结构.引用其实就是c中的指针,只不过perl中对指针,也就是一个地址的声明和取值有自己的一套方法. 1.先复习普通标量的引用方法: [vagrant ...
- perl 引用(数组和hash引用) --- perlreftut - Mark 的一个简单的'引用'教程 ---Understand References Today. --Mark Jason Dominus, Plover Systems (mjd-perl-ref+@plover.com)
https://blog.csdn.net/fangwei1235/article/details/8570886 首页 博客 学院 下载 论坛 APP 问答 商城 活动 VIP会员 招聘 ITeye ...
- Java入门(6)——集合、基本数据类型和引用数据类型的相互转换
集合: 1.HashMap ----> 类 概述: 通过key可以找到value, key就是键, values就是值. 俗称键值对. 特点: 无序的 值可以重复 键不可以重复的 如 ...
- 6、perl创建模块(Exporter)及路径 引用 嵌套 查询模块
参考博客:http://www.cnblogs.com/xudongliang/tag/perl/ 1.perl 模块的创建以及制定perl 模块的路径 (1)创建一个Myfun.pm模块. #/us ...
随机推荐
- Python 踩坑之旅进程篇其三pgid是个什么鬼 (子进程\子孙进程无法kill 退出的解法)
目录 1.1 踩坑案例 1.2 填坑解法 1.3 坑位分析 1.4.1 技术关键字 下期坑位预告 代码示例支持 平台: Centos 6.3 Python: 2.7.14 Github: https: ...
- Error resolving template: template might not exist or might not be accessible是一句缩水报错?
一 thymeleaf在开发的时候本地调试正常,但是在测试环境打成jar包就报这个错误了. 二 template might not exist or might not be accessible ...
- WebStorm技巧-集成命令行工具插件
打开菜单项 File -> Settings- 搜索插件 CMD Support,并安装. 重启WebStorm,在你的项目中新建一个Cmd script 文件,命名为build.cmd ...
- CSS中的定位机制
CSS3 中有三种定位机制 : 普通文档流 (text)| 浮动(float) | 定位(position) 普通文档流 就是CSS中默认的文本文档 普通流中,元素位置由文档顺序和元素性质决定,块级元 ...
- 如何在github中的readme.md加入项目截图
1. 先在之前的本地项目文件夹里创建一个存放截图的文件夹.(如img文件夹) 2. 将新增的内容通过github desktop上传到github中 3. 在github中立马能看到刚刚上传的图片,打 ...
- SQL必知必会-笔记
一.数据库/数据表 数据库(DATABASE):存储有组织的数据的容器; 数据库管理系统(DBMS):数据库软件.开发者通过 DBMS 操纵 DATABASE 表(TABLE):表是一种结构化的文件, ...
- Android笔记--View绘制流程源码分析(一)
Android笔记--View绘制流程源码分析 View绘制之前框架流程分析 View绘制的分析始终是离不开Activity及其内部的Window的.在Activity的源码启动流程中,一并包含 着A ...
- HDU 4055 The King’s Ups and Downs(DP计数)
题意: 国王的士兵有n个,每个人的身高都不同,国王要将他们排列,必须一高一矮间隔进行,即其中的一个人必须同时高于(或低于)左边和右边.问可能的排列数.例子有1千个,但是最多只算到20个士兵,并且20个 ...
- COGS 13. 运输问题4
★★☆ 输入文件:maxflowd.in 输出文件:maxflowd.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 一个工厂每天生产若干商品,需运输到 ...
- java入门第一章——java开发入门
习题解答 一.填空题 (p2)1.java的三个技术平台分别是(java SE.java EE.java ME)(标准.企业.小型) (p3)2.java程序的运行环境简称为(JRE)(开发环境-JD ...