Perl reference list

Jun 26, 2016 • JamesW


转载须注明出处:www.wufei.org

命令行参数

$#ARGV;	# 参数个数
$ARGV[0]

use Getopt::Std;
my %options=();
getopts("od:",\%options); # like the shell getopt, "d:" means d takes an argument
print "-o $options{o}\n" if defined $options{o};
print "-d $options{d}\n" if defined $options{d};
print "Unprocessed by Getopt::Std:\n" if $ARGV[0];
foreach (@ARGV) { print "$_\n"; }

文件

open(FH, "<", $filename) or die $!;
while (<FH>) { print $_; }
while (my $line = <FH>) {}	# 一行行读入, 节省内存
foreach my $line (<FH>) {}	# 读入所有行, 不建议使用
close(FH);

if (-e $filename) {}
-e: 检查是否存在
-s: 返回文件大小
-z: 是否为空
-d: 是否目录

my @files = glob("*.c")

字符串操作

my $s = "Learning Perl is easy\n";
my $sub = "Perl";
index($s, $sub);			# 9
length($sub);				# 4
substr($s, 0, 5) = "Study";	# offset, length
split, join

my $abc = "abc\n";
chomp $abc;					#  除去结尾的\n

# man perlop
$cnt = $sky =~ tr/*/*/;		# count the stars in $sky
tr/a-zA-Z//s;				# bookkeeper -> bokeper

函数

sub PrintList{
	my @list = @_;	# 取参数, 不再是引用
	print "Given list is @list\n";
}
$a = 10;
@b = (1, 2, 3, 4);
PrintList($a, @b);	# Given list is 10 1 2 3 4

sub ff { $_[0] = 2; }	# 参数通过引用传递, 最好别这么用
my $a = 1;
ff($a);
print "$a\n";	# 2

You can return arrays and hashes from the subroutine like any scalar but
returning more than one array or hash normally causes them to lose their
separate identities. So we will use references to return any array or
hash from a function.

sub f {
	my $v = "abc";
	return \$v;	# 返回my变量的引用也没问题
}

临时文件

use File::Temp qw/ tempfile tempdir /;
($fh, $filename) = tempfile("abcd-XXXX", UNLINK => 1);
my $dir = tempdir("abc-XXXX",  CLEANUP => 1 );

执行命令

my $abc = qx(echo abc);	# 输出

my @args = ("command", "arg1", "arg2");
system(@args);
print $?;

正则表达式

排序

my @nums = sort { $b <=> $a } qw(1 11 2 22 10 100);
my @chars = sort { $b cmp $a } qw(perl sort function alphabetically);

my @nums = sort compare qw/1 11 2 22 10 100/;
sub compare {}

变量

@array = (1, 2, 3);
$array[0] == 1;
%hash = (key1 => value1, key2 => value2);
$hash{key1} == value1;
keys %hash, values %hash

$scalarref = \$foo;
$arrayref  = \@ARGV;
$arrayref  = [1, 2, 3];
$hashref   = \%ENV;
$hashref   = {key' => value};

$$scalarref
@$arrayref, $arrayref->[0]
%$hashref, $hashref->{key}

数组操作

@array = (1,2,3);
$size = @array;
$size = scalar @array;

@coins = ("Quarter","Dime","Nickel");
push(@coins, "Penny");		# add one element at the end of the array
unshift(@coins, "Dollar");	# add one element at the beginning of the array
pop(@coins);				# remove one element from the last of the array.
shift(@coins);				# remove one element from the beginning of the array.

@days = qw/Mon Tue Wed Thu Fri Sat Sun/;
@weekdays = @days[3,4,5];
@weekdays = @days[3..5];

$var_string = "Rain-Drops-On-Roses-And-Whiskers-On-Kittens";
@string = split('-', $var_string);
$string1 = join( '-', @string );

@odd = (1,3,5);
@even = (2,4,6);
@numbers = (@odd, @even);	# 函数传参也是如此

Operators

2**3				# 8
3/2					# 1.5
==, !=, <=>, > ...	# 数字比较
lt, gt, cmp, ...	# 字符串比较
add, &&				# 没有区别
'x' x 3				# xxx

q{123}				# 'abcd'					
qq{123}				# "abcd"
qx{123}				# `123`