Perl语言学习(八)Perl文件操作

八、Perl文件操作

1、Perl句柄

  • 句柄的定义
    • Perl通过句柄和“外面”的世界连接
    • 句柄是一个顺序号,对于打开的文件是唯一的识别依据
    • 是一种特殊的“数据类型”
    • 间接文件句柄
      • 任何有效的Perl标识符,由大小写字母、数字和下划线字符组成
      • 不同于其他变量的是,简洁Perl文件句柄没有标志性的前缀
      • 经常以全部大写字母表示他们
      • 输入输出句柄:STDIN, STDOUT和STDERR
  • 输入输出句柄
    • STDIN
      • 标准输入,默认的输入间接Perl文件句柄
      • 文件句柄读取的数据取自用户选择的输入设备,通常是键盘
print "please input the data\n";
my $data = <STDIN>;
print "The data is $data\n";
  • STDOUT
    • 标准输出,默认的输出间接Perl文件句柄
    • 发送给该句柄的数据在用户指定的输出设备上显示,通常为命令窗口
print "Here is the example\n";
print STDOUT "Here is the example";
  • STDERR
    • 标准错误,用于错误信息、诊断、调试和其他类似的偶发输出
    • 默认情况下,STDERR和STDOUT使用相同的输出设备
use warnings;
my $data1 = "abc";
my $data2 = abc;  ## error
print $data1;
print $data2;
  • 自定义句柄
    • 由用户自己指定文件,获取该文件的标识

2、打开文件

  • 使用open函数打开文件

    • 语法:open(filehandle, pathname)
    • 第一个参数为文件句柄
    • 第二个参数为文件路径名
    • 打开成功,返回一个非0值,否则返回一个undef(假)
    • 路径名:
      • 不指定路径名时,默认使用当前目录下的文件
      • 若要打开位于另一个目录中的文件,必须使用路径名
      • 设定路径名时,需要根据操作系统而定

        《Perl语言学习(八)Perl文件操作》 image.png

  • 编程习惯

    • 编写程序时,不可盲目自信
    • 为了避免致命错误,应该进行防错性编程
    • 使用文件句柄时,应确保操作成功后再进行下一步
    • die函数:
      • 在Perl中,die函数可以用来在出现错误时停止解释程序的运行
      • 并放出一条有意义的出错信息:died at scriptname line xxx
      • 语法:open(FILE, pathname) || die;或者open(FILE, pathname) or die;
      • 函数也可以带自定义的参数,这些参数将会取代默认消息被输出,die “info”
      • 使用”$!”得到系统所需要的最后一个操作的出错信息
      print "script start\n";
      open(MYFILE, "test.txt) || die "This file not exists";
      open(MYFILE, "test.txt) || die "Cannot open myfile: $!\n";
      
    • warn函数:
      • 用来发出一个警告
      • 不会终止程序的运行
         if(!open(FILE, "test.txt")){
             warn "cannot read the file: $!";
             else {
                 print "start to read the file";
             }
         }
         print "here is another command\n";
        

3、读写文件

  • 读入文件
    • 使用文件输入操作符(尖括号运算符、钻石符)读取文件
    • 语法:
      • open(FILE, “filename”) || die “Can’t open the file”;
      • $line = <FILE> ##读取文件
        open(FILE, "test.txt") || die "can't open the file:$!\n";
        $line = <FILE>;
        print "$line\n";
        close FILE;
        
    • 说明:
      • 尖括号运算符每次读取文件的一行输入
      • 文件读取完时,返回值为undef
      • 对于多行数据文件,需要利用循环结构来读取所有数据
        open(FILE, "test.txt") || die "can't open the file:$!\n";
        while($a=<FILE>){
            print $a;
        }
        
      • 通过数组一次接收所有输入数据(对于小文件)
        open(FILE, "test.txt") || die "can't open the file:$!\n";
        my @array = <FILE>;
        print @array;
        
  • 写入文件
    • 语法:
      • open(FILE, “>pathname”) ##覆盖写入文件
      • open(FILE, “>>pathname”) ## 追加写入文件
    • 写入具体内容
      • 使用print函数
      • 语法为:print filehandle LIST
      • 注意,上面的语句中没有逗号隔开
  • 关闭文件句柄
    • 使用close(FILE)
    • 每次打开一个文件句柄时,读写结束后尽量关闭文件句柄

4、文件测试运算符

  • 测试文件的必要性
    • 实际的读写速度比较慢
    • 读写时容易产生错误
    • Perl提供了文件测试运算符
  • 文件测试运算符

    《Perl语言学习(八)Perl文件操作》 image.png

  $myfile = "./test.txt";
  if (-w $myfile){
      print "we can write it\n";
      }
      else {
          print "we can't write it\n";
      }
  print (-s $myfile);

《Perl语言学习(八)Perl文件操作》 image.png

  $myfile = "./test.txt";
  print (-M $myfile);

参考来源:https://www.jikexueyuan.com/course/2081.html

    原文作者:Davey_63dc
    原文地址: https://www.jianshu.com/p/ceea01d244ed
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞