1.
chmop($line=<STDIN>) ; #读取下一行,截掉换行符。
2.
while(defined($line=<STDIN>) {
print " $_ ”;
}
3.
while(<STDIN>) {
print ”$_";
}
等价于
while(defined($_ = <STDIN>) {
print " $_";
}
4.钻石操作符 <> 时行输入操作符的特例。不从键盘取得输入,而是用户指定位置。
$ ./my_programe fred barney betty
while (defined($line =< >) {
chomp($line);
print ""
}
等价于
while(<>) {
chomp;
print " $_";
}
5.调用参数
<> 不会去检查命令行参数,其参数来自数组 @ARGV #Perl 解释器事先建立起来的特殊数组。
@ARGV = qw /a b c d/;
while (< > ) {
chomp;
print "$_ in some stooge-like file !";
}
6.输出到标准输出
print @C #abcd
print "@C" #a b c d 有空格
一般情况下程序的输出先到缓冲区,积攒起来,量大再造访外部设备。
print <> #相当于Unix的cat
print sort <> # 相当于Unix的sort
printf 格式化输出。和C语言类似。
7.打开文件句柄。
Perl 提供的默认文件句柄:STDIN STDOUT STDERR
其他句柄,使用open操作符告诉Perl
open CONFIG,'dino' #打开文件dino,从句柄CONFIG读取
open CONFIG,'<dino' # < 为只读
open CONFIG ,'>fred' #打开文件句柄CONFIG,并输入到新的文件fred.
open CONFIG ,'>>fred' #追加式打开,若fred存在则追加在尾部,若不存在,则创建再写入。
my $selected_output = 'my_output';
my $sucess = open LOG , "> $selected_output";
if (! $sucess) {
#
}
8.关闭句柄 close LOG
9.die处理致命错误
0 sucess
1 语法错误
2 处理程序时发生错误
3 找不到某个配置文件
if (!open LOG,'>>' ,'logfile') {
die "can not create logfie : $!" # $!指可读的系统错误信息,如permission denied / file not find
}
Die 自动显示Perl 程序名和行号;
若不想显示则在末尾加 换行符。
if(@ARGV < 2) {
die "Not enough argumeng " #不显示程序名和行号
}
10.改变默认文件句柄
select BD
$| =1; # 立刻刷新输出,不经过缓冲区缓存。
print BD " i hope this will come true!“