perl重点基础知识整理

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

                        perl重点基础知识整理

#若无特殊说明,以下都是基于ubuntu 13.04版本

1:终端下运行perl脚本:

step1:

在桌面新建一个perl脚本,输入:

#! /usr/bin/perl

print
"hello world!";

step2:

打开终端:

hqh@ubuntu:~$ cd ~/桌面

hqh@ubuntu:~/桌面$ sudo chmod
a+x h.pl

hqh@ubuntu:~/桌面$ ./h.pl

hello world!hqh@ubuntu:~/桌面$

看到输出:  hello world!

2:模块的安装:

http://search.cpan.org/ 搜到模块然后下载至 /home/hqh/下载

cd ~/下载

tar zxvf URL-Signature-0.03.tar.gz

cd cd URL-Signature-0.03/

perl Makefile.PL

make

make install

3:defined,undef,exists函数:

#! /usr/bin/perl

my $cin=undef;

if (defined($cin))

{    print
"The input was $cin";

}else

{

    print
"No input available! \n";

}

my %hash1=("a"=>1,"b"=>2,"c"=>3);

print
exists($hash1{"a"}),"\n";

print
exists($hash1{"d"}),"\n";

print
defined($hash1{"a"})."\n";

说明:

exists主要针对hash,判断是否存在key

如果一个变量为undef,则definded判断该变量为0,其他情况为1

4:hash解析

#! /usr/bin/perl

#hash几种解析方式

my %hash1=(‘a‘,2,‘b‘,3);

while(my($k,$v)=each%hash1){

    print
"$k--->$v";

    print
"\n";

}

#hash长度

my $len=scalar
keys%hash1;

print
$len,"\n";

foreach
my $key1(keys
%hash1){

    print
$key1,"-->",$hash1{$key1};

    print
"\n";

}

5:深入理解 @_以及$_

@_:默认的参数数组

$_:默认的变量

自己写了一段下面的程序,清晰了!!嘿嘿

#! /usr/bin/perl

sub myfun2{

 A1: for(my
$i=0;$i<=4;$i++){

        print
$_[$i],"\n";

    }

    my
$str1=shift
@_;

    print
@_,"\n";     

    print
scalar @_,"\n";                                                                 

    my
($str2,$str3)=@_[2,3];              

    print
"my str1 is $str1 \n";           

    print
"str2 is $str2 and str3 is $str3";

    foreach
my $keys(@_){                    

        print
"key analysis",$keys,"\n";

    }

  A2:   for(my
$i=0;$i<=3;$i++){

        print
$_[$i],"\n";

    }

}

print
&myfun2(1,2,3,4,5),"\n";

调用sub程序,

将1到5默认复制给@_

shift
@_,取出@_数组的第一个元素1,并且@_自动更新为(2,3,4,5);

@_的第3,4个元素复制给str2,str3

遍历数组

$_表示默认的变量,可以看出,变量的个数与@_的长度一样,看A1和A2两段for循环就可以知道,随着@_做了shift后,长度发生变化,而$_的个数也随之变化

6:index,rindexsplit函数

#! /usr/bin/perl

my $url="http://www.qq.com.sina.com.cn//.wwwsohu.com.cn.oo.yellow";

print
$url,"\n",length($url),"\n";

print
index($url,"www"),"\n";

print
index($url,"www",8),"\n";

print
index($url,"."),"\n";

print
rindex($url,"."),"\n";

my @list=split(/\./,$url);

print
@list;

print
scalar @list,"\n";

my @list1=split(/\\\\/,$url);

print
@list1;

7:高级排序---针对hash哦

my %score = (

"barney"=>100,

"fred"=>205,

"dino"=> 30,

"yellow"=>100);

my @winners sort
by_score keys
%score;

sub
by_score{

    $score{$b}<=>$score{$a}

}

print
@winners;

sub
by_score_or_name{

    $score{$b}<=>$score{$a}

    or

    $a
cmp $b;

    }

    

my @winners1 sort
by_score_or_name keys
%score;

print
@winners1;

    

8:之前的学习笔记

#! /usr/bin/perl

my $str="huangqihao";

print
$str x 4;

print
"$str \n" x 4;

print
5 x 4;

print
"\n";

print
2.5%2;

print 
"\n";

print
2.5**7;

print
"\n";

#交换两个变量

($a,$b)=($b,$a);

print
$a,$b;

print
"\n";

#qw符号

($a,$b,$c)=qw !abc 123 434!;

print
"$a\n$b\n$c\n";

($a,$b,$c)=qw .abc 123 434.;

print
"$a\n$b\n$c\n";

($a,$b,$c)=qw :abc 123 434:;

print
"$a\n$b\n$c\n";

#pop,push

@www=(‘ab‘,‘cd‘,‘ef‘);

print
pop(@www);

push(@www,0);

print
@www;

#pop 和 push的第一个参数都为数组变量

#shift,unshift

@www=(‘ab‘,‘cd‘,‘ef‘);

print
shift(@www);

unshift(@www,4);

print
@www;

#shift和 unshift的第一个参数都为数组变量

#数组插入字符串

$ii="[email protected] sdjfkl";

print
$ii;

#reverse,sort

@www=(‘db‘,‘cd‘,‘ec‘);

print
reverse(@www);

print
sort(@www);

print
"\n";

#              

#读文件

open(FD,"/home/hqh/桌面/2.pl")||die("can not open the file");

@aa=<FD>;

print
@aa;

foreach
(@aa){

    print
"$_\n";

}

#写文件

open(HD,">>/home/hqh/桌面/3.pl")||die("can not open the file");

#HD为写入句柄

print
HD "@aa";

open(ND,"/home/hqh/桌面/3.pl")||die("cant not open the file");

@bb=<ND>;

print
@bb;

#追加文件

open(ZJ,">>/home/hqh/桌面/3.pl")||die("can not open the file");

print
ZJ "@aa";

open(ZJ,"/home/hqh/桌面/3.pl")||die("can not open the file");

@cc=<ZJ>;

print
@cc;

#关注循环体中last,next,redo的操作!!!

$sum1=0;

for($i=0;$i<=10;$i++){

    $sum1=$sum1+$i;

    last
if($i==6);

}

print
"sum1=$sum1\n";

##只加到5,遇到6,就跳出循环啦!!

$sum2=0;

for($i=0;$i<=10;$i++){

    next
if($i==6);

    $sum2=$sum2+$i;

}

print
"sum2=$sum2\n";

#######6木有加进去,其他都加进去了

$sum3=0;

for($i=0;$i<=10;$i++){

    print
"hello,redo is here!\n";

    redo
if($i==6);

    $sum3=$sum3+1;

}

print
"sum3=$sum3\n";

############一直在循环print hello..

#三元操作符,等于R语言中ifelse函数

$a=1+1>3 ? " larger"
:" smaller";

print
$a;

#三元操作符,等于R语言中ifelse函数

$a=1+1>3 ? " larger"
:" smaller";

print
$a;

#perl目录文件操作

chdir
"/home/hqh/桌面/perlperl" or die
"cannot chdir to /etc: $!" ;

@fileparameters=glob"*";

print
@fileparameters;

chdir
"/home/hqh/桌面/perlperl/first perl" or die "cannot chdir to /etc: $!" ;

@fileparameters=glob"*";

print
@fileparameters;

chdir
"/home/hqh/桌面/perlperl" or die
"cannot chdir to /etc: $!" ;

#解析目录

opendir(HD ,"/etc") || die
"can not opendir! ";

@aa=readdir(HD);

print
@aa;

foreach
$tt(@aa){

    print
"$tt\n";

}

closedir
HD;

#删除操作 unlink 与glob结合

#unlike glob "*.pm";

#重命名 rename

#创建和删除目录 mkdir rmdir

#修改权限 chmod

#改变所有者 chown 1004 ,100,glob "*.pm" ,1004表示user的ID,调用 getpwnam 函数,将名字转换为数字,而对应的 getgrnam

#将组名转换为数字

#index函数,查找子串在主串中的位置!

#substr函数

#sprintf函数,返回请求的字符串,不被打印出来,用于赋值,比较豪!

perl重点基础知识整理

时间: 2024-08-24 03:35:07

perl重点基础知识整理的相关文章

Java基础知识整理(一)

概述 公司业务需要,产品既要有.NET又需要Java,没得选择,只能业余时间学习Java,整体觉得Java也.NET还是很相似的,只是语法有差别,差别也不是很大,这就将学习Java的基础知识整理下,以便于自己的学习.作为个.NET程序猿也可以学习Java ,毕竟技多不压身,学习多也要精通. 开发工具 eclipse ,开发java类似.NET 需要装JDK类似.NET Framework. Java开发工具eclipse设置 1.设置字体:window设置: 2.设置快捷键:window--ke

DIV+CSS网页布局常用的一些基础知识整理

CSS命名规范一.文件命名规范 全局样式:global.css:框架布局:layout.css:字体样式:font.css:链接样式:link.css:打印样式:print.css: 二.常用类/ID命名规范页 眉:header内 容:content容 器:container页 脚:footer 版 权:copyright 导 航:menu主导航:mainMenu子导航:subMenu 标 志:logo标 语:banner标 题:title侧边栏:sidebar 图 标:Icon注 释:note

Kali Linux渗透基础知识整理(二)漏洞扫描

Kali Linux渗透基础知识整理系列文章回顾 漏洞扫描 网络流量 Nmap Hping3 Nessus whatweb DirBuster joomscan WPScan 网络流量 网络流量就是网络上传输的数据量. TCP协议 TCP是因特网中的传输层协议,使用三次握手协议建立连接.当主动方发出SYN连接请求后,等待对方回答SYN+ACK ,并最终对对方的 SYN 执行 ACK 确认.这种建立连接的方法可以防止产生错误的连接,TCP使用的流量控制协议是可变大小的滑动窗口协议. 连接建立 TC

JAVA基础知识整理

一.首先先明白get与post的基本定义和区别: 这是两种在客户端和服务器端进行请求-响应的方法. 1get:从指定的资源请求数据. 2post:向指定的资源提交要处理的数据. get基本上用于从服务器取回数据,注意:get方法可能返回缓存数据. post可以从服务器上获取数据,不过,post方法不会缓存数据,并且常用语连同请求一起发送数据. 二. Jquery $.get()方法. $.get()方法通过Http Get发起请求,从服务器上请求数据. 语法:&.get(URL,callback

[HTTP] 高级基础知识整理

HTTP 高级基础知识 整理 HTTP 高级基础知识,包括 Cookie / Session / localStorage / sessionStorage / Cache-Control / Expires / Etag 等 Cookie cookie :wiki Cookie(复数形态Cookies),又称为"小甜饼".中文名称为"小型文本文件",指某些网站为了辨别用户身份而储存在用户本地终端(Client Side)上的数据(通常经过加密) -wiki coo

c语言基础知识整理(二)

C语言的基础知识 对C语言的基础认识: 1.C语言编写的程序称为源程序,又称为编译单位. 2.C语言书写格式是自由的,每行可以写多个语句,可以写多行. 3.一个C语言程序有且只有一个main函数,是程序运行的起点. 4.每个C语言程序写完后,都是先编译,后链接,最后运行.(.c---à.obj---à.exe)这个过程中注意.c和.obj文件时无法运行的,只有.exe文件才可以运行. (程序编辑-程序编译-程序连接-程序运行) 标识符: 1.标识符: 合法的要求是由字母,数字,下划线组成.有其它

C语言基础知识整理

用一个简单的c程序例子,介绍C语言基础知识的基本构成.格式.以及良好的书写风格,使小伙伴对c语言有个初步认识. 例1:计算两个整数之和的c程序: #includemain(){int a,b,sum;  a=20;       b=15;       sum=a+b;   printf("a=%d,b=%d,sum=%d\n",a,b,sum); } 重点说明: 1.任何一个c语言程序都必须包括以下格式: main(){   } 这是c语言基本知识结构,任何一个程序都必须包含这个结构.

密码学基础知识整理

最近在研究密码学加密,签名方面的东西.经过几天的学习对一些基础知识进行一下整理 PKI:PKI是Public Key Infrastructure的首字母缩写,翻译过来就是公钥基础设施,在X509标准中PKI为支持共有密钥管理并且支持认证.加密.完整性.可追究性服务的基础设施. CA:CA是 Certificate Authority首字母的缩写,翻译过来的意思是:证书认证授权机构,其主要作用是负责发放管理数字证书的具有权威性的第三方机构.CA通过证书证实他人的公钥信息,证书上有CA的签名.用户

[转]基础知识整理

[写在前面]本页面仅涉及基础知识的梳理,比如算法与数据结构,操作系统,数据库,C/C++等,这一块主要是确定性知识,仅有对或者不对之说,可探讨的地方较少,对于找工作的同学会比较有用.对于已工作的同志,可绕过此页面,直接前往“专业知识处理”. 原文地址:http://dongxicheng.org/knowledges-carding/ ————————————————————————————————————————————- 1. 数据结构与算法 1.1 书籍 (1)算法导论 (2)编程之美 (