输入与输出 Perl 第五章

 

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 程序名和行号;

若不想显示则在末尾加\n 换行符。

if(@ARGV < 2) {

die "Not enough argumeng \n"  #不显示程序名和行号

}

 

10.改变默认文件句柄

select BD

$| =1;   # 立刻刷新输出,不经过缓冲区缓存。

print  BD " i hope this will come true!“

 

转载于:https://www.cnblogs.com/lelin/p/11207148.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值