[macg@localhost perltest]$ vi tip.pl #!/usr/bin/perl print"input:"; while(chomp($input=<>)) { print"your input is $input \n"; if ($input=="q") { print "choose q \n";last;} print "input:"; |
[macg@localhost perltest]$ ./tip.pl input:x your input is x choose q |
while(chomp($input=<STDIN>)) |
Do you want to change eth0:2 's ip address ? 回车 Argument "" isn't numeric in numeric eq (==) at ./address.pl line 77, <STDIN> line 2. 对×××变量$input==$i,如果$input是回车,并不走else,而是报错 |
while(chomp($input=<STDIN>))
which interface you want to config ? choice a number 1 2 3 4 q:1
Do you want to change eth0 's ip address ?
print "純金 ", $v1;
print $str, "\n\n";
print '12345 大家來跳舞' . " hello world";
結果變成:
12345 大家來跳舞 hello world
print "OK" x 4;
結果變成:
OKOKOKOK
因为可能+就是真加了(数字相加),而不是字符串合并
$v1 = 99; $v2 = '121'; print $v1 + $v2; | $v1 = 99; $v2 = '121'; print $v2 . $v1; |
220 | 12199 |
$min=1; $date="date "."0".$min; print $date,"\n"; |
[root@ntracker mac]# ./tip.pl date 01 |
$str="abCD99e"; $str = uc($str); | $str="abCD99e"; $str = lc($str); |
[macg@localhost perltest]$ ./tip.pl ABCD99E | [macg@localhost perltest]$ ./tip.pl abcd99e |
#!/usr/bin/perl $str="abCD99e"; $strlen=length($str); print $strlen,"\n"; |
[macg@localhost perltest]$ ./tip.pl 7 |
#!/usr/bin/perl $str = "ABCDEFG1234567"; $a = substr $str, 0, 5; print $a,"\n"; |
[macg@localhost perltest]$ ./tip.pl ABCDE |
$a = substr $str, -4, 2; |
[macg@localhost perltest]$ ./tip.pl 45 |
#!/usr/bin/perl $str = "ABCDEFG1234567"; $a = "12"; $pos=index($str,$a); print $pos,"\n"; |
[macg@localhost perltest]$ ./tip.pl 7 |
#!/usr/bin/perl $str = "ABCDEi FG12i 345 6 7"; @array=split(/ /,$str);按空格分 foreach (@array) { } |
[macg@localhost perltest]$ ./tip.pl ABCDEi FG12i 345 6 7 |
@array = split (/ +/, $line);
[macg@localhost perltest]$ vi tip.pl #!/usr/bin/perl $str = "ABCDEi FG12i @array=split(/\t /,$str); foreach (@array) { } |
[macg@localhost perltest]$ ./tip.pl ABCDEi FG12i 345 6 7 |
因为同时满足TAB和空格的只有一处
所以必须加[ ]
@array=split(/[\t ]/,$str); |
[macg@localhost perltest]$ ./tip.pl ABCDEi FG12i 345 6 7 |
语法:join($string,@array)
@array=qw(one two three); | $total="one,two,three"; |
@array=qw(one two three); $total=join(":",@array); | $total="one:two:three"; |
@array=("one","on","in"); $count =grep(/on/,@array); 查询结果赋值给单变量 | @array=("one","on","in"); @result=grep(/on/,@array); 查询结果赋值给数组 |
2 | one on |