ITEM | perl | bash |
---|---|---|
变量定义方法 | my | set |
perl是很随意的语言,为了避免不必要的麻烦,可以在程序开头加如下command
变量未初始化时默认赋值为undef
my $undef = undef;
print $undef; # 打印空字符串"",并且抛出一个警告
# 隐式的undef(译者注:未初始化的变量初值默认为undef):
my $undef2;
print $undef2; # 打印"",并且抛出完全一样的警告
my $num = 4040.5;
print $num; # "4040.5"
$what =“brontosaurus steak”;
$n = 3;
print “fred ate $n $whats.\n”; #不是steaks,而是$whats 的值
print “fred ate $n ${what}s.\n”; #现在是使用变量$what
print “fred ate $n $what”. “s.\n”; #另一种方法
print ‘fred ate ’. $n . ‘’. $what . “s.\n”; #一种复杂的方法
perl没有内置布尔类型,通常被判定为false的返回值有:
判定为true时通常返回值为1
ITEM | 数字 | 字符串 |
---|---|---|
等于 | == | eq |
不等 | != | ne |
小于 | < | lt |
大于 | > | gt |
小于或等于 | <= | le |
大于或等于 | >= | ge |
# 经典错误
print "yes" == "no"; # "1" 并且抛出两个警告,按数值方式参与运算,两边求值结果都是0
35 != 30+5 #false
35 == 35.0 #true
‘35’eq‘35.0’ #false (按照字符串比较)
‘fred’lt ‘barney’ #false
‘fred’lt ‘free’ #true
‘fred’eq ‘fred’ #true
‘fred’eq ‘Fred’ #false
‘’gt ‘’ #true
$line = <STDIN>;
if($line eq “\n”){
print “That was just a blank line!\n”;
}
else{
print “That line of input was: $line”;
}
$madonna = <STDIN>;
If ($defined ($madonna)){
print “The input was $madonna”;
}else{
print “No input available!\n”;
}
条件为真时执行code block
if(condition1){
}elseif(condition2){
}else{
}
条件为假时执行unless code block
unless(condition){
}else{
}
$count = 0;
while ($count < 10) {
$count + = 2;
print “count is now $count\n”; #打印出2 4 6 8 10
}
foreach $rock (qw/ bedrock slate lava /){
print “One rock is $rock.\n”; #打印出3 种rocks
}
@rocks = qw/ bedrock slate lava /;
foreach $rocks(@rocks){
$rock = “\t$rock”; #@rocks 的每一个元素前加入一个tab
$rock . = “\n”; #每一个元素后加一个换行符
}
print “The rocks are:\n”,@rocks; #每一个元素都被缩进了,并且一个元素占一行
和c语言for循环类似
for(my $i=0;$i<=number;$i++){
}
do while会执行至少一次(条件为假时)
my $a = 10;
do {
print "the value is $a now\n";
$a--;
}
while($a >= 1);
print "the value is less than 1";
整体赋值
my @array = (
"print",
"these",
"strings",
"out",
"for",
"me", # 末尾多余的逗号语法上是允许的
)
单独赋值
$array[0] = "print";
用$符号存取数组中的值,因为数组中的每个元素是标量
[下标为正]
print $array[0]; # "print"
print $array[1]; # "these"
print $array[2]; # "strings"
print $array[3]; # "out"
print $array[4]; # "for"
print $array[5]; # "me"
print $array[6]; # 返回undef,打印""并且抛出一个警告
[下标为负]
print $array[-1]; # "me"
print $array[-2]; # "for"
print $array[-3]; # "out"
print $array[-4]; # "strings"
print $array[-5]; # "these"
print $array[-6]; # "print"
print $array[-7]; # 返回undef,打印""并且抛出一个警告
($fred, $barney, $dino) = (“flintstone”, “rubble”, undef);
($fred, $barney) = ($barney, $fred) #交换两个变量
($betty[0],$betty[1]) = ($betty[1],$betty[0]);
($fred, $barney) = qw <flintstone rubble slate granite>; #两个值被忽略了
($wilma,$dino) = qw[flintstone]; #$dino 为undef
@rocks = qw / bedrock slate lava /;
@tiny = (); #空表
@giant = 1..1e5; #包含100,000 个元素的表
@stuff = (@giant, undef, @giant); #包含200,001 个元素的表
@dino = “granite”;
@quarry = (@rocks, “crushed rock”, @tiny, $dino);
@copy = @quarry; #将一个数组中的值拷贝的另一个数组中
tips:
my $string="This is a kind of dynamic array";
my @array;
@array=split(‘a‘,$string);
foreach(@array)
{
print "$_ \n”;
# This is a special variable which stores the current value.
}
#result:
Output:
This is
kind of dyn
mic
rr
y
一般形式:
sub marine {
$n + = 1; #全局变量$n
print “Hello, sailor number $n!\n”;
}
调用子程序方法:&sub_function_name,返回值为最后一个被计算的表达式。
子程序可以通过调用参数的方式,简化不必要的参数定义.
#一般表示方法
my $a,$b;
sub max_num{
if($a > $b){
print "$a is the larger number";
}
else{
print "$b is the larger number";
}
}
#带参数表示方法
sub max_num{
if($_[0] > $_[1]){
print "$_[0] is the larger number";
}else{
print "$_[1] is the larger number";
}
}
子程序调用参数是参数会被存储在@_这个数组中,所以参数的第一个值存储在@_[0],第二个存储在@_[1]等依次类推
子程序中的多余参数会自动忽略,缺少的参数会用undef来表示。
sub max_num{
my ($m,$n)=@_;
if($m > $n){
print "$m is the largest number";
}else{
print "$n is the largest number";
}
}
# result:6 is the largest number
整体赋值
%hash = (‘jan‘ => 1,‘feb‘ =>2,‘mar‘ => 3 );
%hash = (‘jan‘,1,‘feb‘,2,‘mar‘,3);
单独赋值
读取hash value
delete hash key
获得hash key/value list
将hash value/key值赋予数组时,数组中的元素顺序可能总是不同的。
类似于linux cd command,directory要加双引号
chdir EXPR
change directory to EXPR;
#!/usr/bin/perl
chdir "/usr/home";
# Now you are in /usr/home dir.
chdir;
# Now you are in home directory /user/home/tutorialspoint
mkdir EXPR,MODE
创建路径并加权限
#!/usr/bin/perl -w
$dirname ="/tmp/testdir";
mkdir $dirname, 0755;
使用system()执行linux command,command需要加双引号
system("ln -sf dirs");
Q:perl中的私有变量如何设定的?子程序中my ($a,$b)是私有变量,my $a,$b是私有变量吗?可以这样定义吗?
A:
代码块中{}内的变量都属于私有变量;
my $a,$b; 仅定义了$a;如果要全部定义,形式为my ($a,$b);
($a) = @_; #数组第一个元素赋值给$a;
$a = @_; #$a为数组的个数;
原文:https://www.cnblogs.com/movit/p/14127899.html