Perl 开发的学习-2
字符串变量,需要注意单引号与双引号的不同,这一点在Shell里也一样,就是单引号不进行转义# cat str.pl
#!/usr/bin/perl
$str="short";
$string="long";
print "match longest $string\n";
print "match shortlets ${str}ing\n";
# perl str.pl
match longest long
match shortlets shorting
#
root@windriver-machine test]# cat esc.pl
#!/usr/bin/perl
print "bell ring:\a\n";
print "back#\bspace\n";
print "copy\rabc\n";
print "abc\tdef\n";
print "the\$var\n";
print "a quote \" in string \n";
print "a quote \\ in string \n";
print "\045\n";
print "\x25\n";
print 'the \$var\n';
print 'this is the first line,
this is second line.\n';
# perl esc.pl
bell ring:
backspace
abcy
abc def
the$var
a quote " in string
a quote \ in string
%
%
the \$var\nthis is the first line,
this is second line.\n#
# cat qq.pl
#!/usr/bin/perl
$var=1;
print q(the $var\n);
print qq(is $var\n);
print qq;
# perl qq.pl
the $var\nis 1
string "str"(var) in qq#
# perl undef.pl
2
# cat undef.pl
#!/usr/bin/perl
$r=$und+2;
print "$r\n";
#
# cat undef.pl
#!/usr/bin/perl -w
$r=$und+2;
print "$r\n";
# perl undef.pl
Name "main::und" used only once: possible typo at undef.pl line 2.
Use of uninitialized value in addition (+) at undef.pl line 2.
2
#
root@windriver-machine test]# cat chp.pl
#!/usr/bin/perl
$a="abcd";
chop($a);
print "chop $a\n";
$a="abcd";
chomp($a);
print "chomp $a\n";
$a="abc\n";
chop($a);
print "chop $a|";
$a="abc\n";
chomp($a);
print "chomp $a|";
$a="abc\n\n\n";
$/="";
chomp($a);
print "chomp many $a\n";
$/="cd";
$a="abcd";
chop($a);
print "chop $a|";
$a="abcd";
chomp($a);
print "chomp $a|";
# perl chp.pl
chop abc
chomp abcd
chop abc|chomp abc|chomp many abc
chop abc|chomp ab|#
注意这些函数是直接修改原变量,不是返回值。
操作符与操作数是计算机里的基本的概念,同样在Perl 中也一样。
移位运算首先操作符一定是整数,否则是无法进行移位操作的。
# cat op.pl
#!/usr/bin/perl
print " minus the number before pot:";
$a=25.7%7.6;
print "$a\n";
print "log number is pot:";
$b=(25)**1.5;
print "$b\n";
print "auto increment for string,donot extrand length:";
$a='Z';
$a++;
print "$a\n";
print "the prority of || and or is not same:\n";
$a=1;
$b=1;
$c=($a+=0||$b);
print "$c\n";
$a=1;
$b=1;
$c=($a+=0 or $b);
print "$c\n";
print "=associativity :";
$a=2,$b=1;
$a*=$b+=5;
print "$a\n";
$a=2,$b=1;
($a*=$b)+=5;
print $a;
# perl op.pl
minus the number before pot:4
log number is pot:125
auto increment for string,donot extrand length:AA
the prority of || and or is not same:
2
1
=associativity :12
7#
页:
[1]