zoukankan      html  css  js  c++  java
  • perl 读写文件

    #http://perlmaven.com/open-and-read-from-files

    #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 "$row
    ";
    }
    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 "$row
    ";
    }
    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 = <FILEIN>;
    for my $l (@FileContents){
    print "$l
    ";
    }
    close FILEIN;
    

      

    end

  • 相关阅读:
    c# 定义委托和使用委托(事件的使用)
    c# 继承与多种状态
    ref 参数与out参数
    c# 类
    foreach 语句
    c# 制作弹窗
    c#常用类
    Python中的OS对路径的操作以及应用
    Git 推送文件到远程仓库
    Python基础 函数
  • 原文地址:https://www.cnblogs.com/itech/p/3696895.html
Copyright © 2011-2022 走看看