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

  • 相关阅读:
    WCF
    WCF
    C#
    ASP.NET MVC
    WCF
    关于函数的参数
    关于函数的return
    移动Web开发技巧汇总(转)
    关于reset.css的那些事
    关于iphone自动播放音频和视频问题的解决办法
  • 原文地址:https://www.cnblogs.com/itech/p/3696895.html
Copyright © 2011-2022 走看看