Perl 小骆驼 5章笔记 输入与输出

第五章 输入输出

while (defined($line=)) {
  print "I saw $line";
}
可以简写成:
while () {
  print "I saw $_";
}
类似的有
foreach () {
  print "I saw $_";
}
但底层实现while和foreach有很大区别。while是读一行输入,处理一行。foreach是读完所

有的再处理,所以通常while的性能会好些。


./my_program fred barney betty
运行程序,将处理文件fred, barney和betty
./my_program fred - betty
处理文件fred,标准输入流,和betty.其中-代表标准输入流。

比如
while ($line=<>) {
  chomp($line);
  print "it was $line that I saw!\n";
}
运行程序,调用参数为fred, barney, betty,则会依次输出三个文件中的行。
当前文件名字在$ARGV中。或者:
while (<>) {
  chomp;
  print "it was $line that I saw!\n";
}

@ARGV是perl中的一个特殊数组,包含调用参数的列表。程序开始运行的时候,调用参数就被存储

在@ARGV中。
在启动程序之后,使用<>之前,还有机会修改@ARGV的值。


print @array;  打印出所有元素,一个接一个,,没有空格。
print "@array"; 打印出所有元素,由空格分开。
perl在内插数组时,会在元素之间加入空格。
因此如果字符串包含换行符,则通常print @array;
否则print "@array\n";

print <>;   cat的源程序
print sort <>;   sort的源程序

当没有括号时,print是一个列表操作,将后面所有元素输出。当后面有一个括号时,print为函数

调用,将输出括号中的内容。

printf
格式化输出
比如
printf "Hello,%s: your password expires in %d days!\n",
  $user,$days_to_die;
输出数字 %g
十进制整数 %d
字符串%s


比如这个程序就把一个数组中的所有元素输出,右对齐,长度为10.
#!c:\perl\bin\perl -w
my @items=qw/wilma dino pebbles/;
my $format="The items are:\n".("%10s\n" x @items);
printf $format,@items;
或者更简单:
printf "The items are:\n".("%10s\n" x @items), @items;

句柄:推荐句柄所有字母都大写。
perl自身有六个文件句柄:STDIN, STDOUT, STDERR, DATA, ARGV, ARGVOUT

该程序读输入,然后依次输出
#!c:\perl\bin\perl -w
while (<>) {
  print "$_";
}
运行该程序perl test file2 则会将file1作为输入,file2作为输出。完成将file1内容拷贝到file2。
上述程序也可以写成 print(<>);

文件句柄的打开
open CONFIG, "dino";
open CONFIG, "open BEDROCK, ">fred";
open LOG,">>logfile";

判断是否打开成功
my $success=open LOG, ">>logfile";
if (!$success) {
#打开失败
}

关闭文件句柄
close REDROCK;

当发生严重错误时,可以用die函数来创建自己的严重错误。
if (!open LOG,">>logfile"){
  die "cannot create logfile:$!";
}
$!是错误信息。
die还会自动将perl程序的名字和行数输出在消息的末尾。如果die后面有\n,则不输出名字和行数。
所以如果是用户的错误,则不需要名字和行数。

warn是用来写警告信息的,加上程序名字和行号,并把消息输出到标准错误。但不退出。

使用文件句柄:
if (!open PASSWD,"/etc/passwd"){
  die "How did you get logged in?($!)";
}
while () {
  chomp;
}

print LOG "test log\n";
print STDERR "TEST";
文件句柄和要打印的内容之间没有逗号。

改变默认的输出句柄使用SELECT,比如
select BEDROCK;
print "help\n";将会输出到BEDROCK句柄
记得用完要恢复噢 select STDOUT;

$|=1;在输出操作结束后会立刻清空文件句柄

将出错信息送到私有错误日志:
if(!open STDERR, ">>/home/barney/.error_log") {
  die "cannot open error log for append:$!";
}
这样再有错误信息的时候就会写到log文件中。但是最好不要这么用,最好是./myprogram 2>>/home/barney/error_log


练习题:

1.

#!c:\perl\bin\perl
foreach (reverse @ARGV) {
  open handler, "$_";
  @lines=;
  #print @lines;
  $linenum=@lines;
  foreach $index(0..$linenum) {
    print $lines[$linenum-$index]."\n";
  }
  print "\n";
  close handler;
}

这个更好:

#!c:\perl\bin\perl
foreach (reverse @ARGV){
  #print "$_";
  open handler, "$_";
  @lines=;
  #print @lines;
  @lines=reverse @lines;
  foreach $line(@lines) {
    print "$line\n";
  }
  close handler;
}



2.

print "123456789012345678901234567890\n";
foreach (<>) {
  printf "%10s",$_;
}


3.

#!c:\perl\bin\perl
print "123456789012345678901234567890\n";
@lines=<>;
$num=shift @lines;
chomp($num);
foreach () {
  $format="%".$num."s";
  printf "$format\n",$_;
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11903161/viewspace-688595/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/11903161/viewspace-688595/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值