先验证linux是否安装了perl,使用perl -v测试
[root@localhost shell]# perl -v
This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
(with 33 registered patches, see perl -V for more detail)
Copyright 1987-2012, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
上面说明已经安装了perl环境,如果没有安装,网上搜索相关教程安装。
运行perl程序
可以使用交互式方式,使用-e选项来执行代码,如下
[root@localhost ~]# perl -e 'print "hello world \n"'
hello world
我们也可以变现perl脚本文件,然后赋予执行权限,进行执行,如下所示:
[root@localhost shell]# cat hello.pl
#!/usr/bin/perl
print "hello huangbaokang\n";
赋予执行权限
[root@localhost shell]# chmod u+x hello.pl
执行如下:
[root@localhost shell]# ./hello.pl
hello huangbaokang
以上print语法也可以使用括号来输出字符串
print("hello huangbaokang\n");
perl里的注释跟shell编程类似,用#号注释单行
多行注释用=pod开始 =cut结尾,这个我还是第一次见,跟其他编程语言不一样。
如
#!/usr/bin/perl
# 这是一个单行注释
print "Hello, world\n";
=pod 注释
这是一个多行注释
这是一个多行注释
这是一个多行注释
这是一个多行注释
=cut