perl语言基础学习

Perl 的全称是 Practical Extraction and Report Language ,直译为 “实用报表提取语言”。通过名字可以看出Perl的主要应用是处理文件。

一,运行perl程序

在linux下运行Perl程序有两种方式(示例中在当前目录下创建script.pl):

1、使用“perl 路径“来执行:

perl ./script.pl
2、在脚本文件中指定perl的安装路径后,直接输入”script.pl的路径“来执行:

首先,在script.pl的文件开头加入:

#!/user/bin/perl
如果不知道perl的安装路径可以在linux下使用which报告perl的安装目录:

which perl
-> /user/bin/perl
然后,改变script.pl的权限,允许在linux下执行,最后在linux下输入script.pl路径执行perl程序,这里在当前目录所以使用的是./script.pl。

chmod 755 script.pl
./script.pl
在perl脚本的开头除了安装路径以外经常用到的还有”use strict;“和”use warnings;“前者用来检查潜在的代码错误并终止程序,后者对脚本问题提出预警,相当于命令行中的”-w“:

#!/user/bin/perl
use strict;
use warnings;
也可以直接写作:

#!/user/bin/perl -w use strict
二,基本语法

1、注释

注释有两种方法,一种是使用”#“单行注释,一种使用”=pod“和”=cut“进行多行注释。

#!/user/bin/perl -w use strict

print "Hello World\n"; #comment1
#comment2
#!/user/bin/perl -w use strict
print "Hello World\n";
=pod
comment1
comment2
=cut
上述两种注释方式的执行结果都是一样的。

perl script.pl
-> Hello World
2、单引号与双引号

双引号可以正常解析一些转义字符与变量,而单引号无法解析会原样输出。

#!/user/bin/perl -w use strict
$value = Hello ;
print "$value \n"; #Take an example of double quotation marks
print '$value \n'; #Take an example of single quotation marks
上面的脚本输出结果为

./script.pl
-> Hello
-> $value \n
对于变量和换行符为例的转义字符单引号不会进行解析,但单引号可以使用多行文本双引号则不行。

#!/user/bin/perl -w use strict

print "double \n";
print "quotation \n";

print 'single
quotation';
输出结果为:

./script.pl
->double
quotation
single
quotation
3、转义字符

一些特殊符号想要输出就需要使用”\“反斜线进行转义,例如上面例子使用的变量符号”$“,如果钱前面加上”\“,那么$value就不会被其实际值”Hello“所代替。

#!/user/bin/perl -w use strict
$value = Hello ;
print "$value \n";
print "\$value \n"; #Take an example of "\"
上述示例输出结果为:

Hello
$value
perl中常见的转义符如图1:

 

图1.转义符

4、标识符

在程序中使用的变量名,常量名,函数名,语句块名等统称为标识符。其包括英文字母(a-z和A-Z),数字(0-9)和下划线 (_),标识符以字母或下划线开头,且区分大小写。
————————————————

 

 

参考:https://www.cnblogs.com/feihongwuhen/archive/2012/10/12/7169781.html

https://blog.csdn.net/spx1164376416/article/details/124451177

 

posted @ 2024-02-06 10:09  konglingbin  阅读(21)  评论(0编辑  收藏  举报