欢迎来到山村网

perl读写文件代码实例

2019-03-02 12:25:27浏览:689 来源:山村网   
核心摘要:  这篇文章主要介绍了perl读写文件代码实例,本文直接给出实现代码,需要的朋友可以参考下  #mode operand create truncate 

  这篇文章主要介绍了perl读写文件代码实例,本文直接给出实现代码,需要的朋友可以参考下

  #mode operand create truncate

  #read <

  #write > yes yes

  #append >> yes

  Case 1: Throw an exception if you cannot open the file:

   代码如下:

  use strict;

  use warnings;

  my $filename = 'data.txt';

  open(my $fh, '<:encoding(UTF-8)', $filename)

  or die "Could not open file '$filename' with the error $!";

  while (my $row = <$fh>) {

  chomp $row;

  print "$rown";

  }

  close($fh);

  Case 2: Give a warning if you cannot open the file, but keep running:

  代码如下:

  use strict;

  use warnings;

  my $filename = 'data.txt';

  if (open(my $fh, '<:encoding(UTF-8)', $filename)) {

  while (my $row = <$fh>) {

  chomp $row;

  print "$rown";

  }

  close($fh);

  } else {

  warn "Could not open file '$filename' $!";

  }

  Case 3: Read one file into array

  代码如下:

  use strict;

  use warnings;

  my $filename = 'data.txt';

  open (FILEIN, "<", $filename)

  or die "Could not open file '$filename' with the error $!";

  my @FileContents = ;

  for my $l (@FileContents){

  print "$ln";

  }

  close FILEIN;

(责任编辑:豆豆)
下一篇:

perl中使用signal(信号)实例

上一篇:

Shell脚本传递参数的3种方法比较

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com