Perl教程
Perl控制语句
Perl高级

Perl 文件处理

Perl 文件处理

文件处理是任何编程语言中最重要的部分。文件句柄是与文件名相关联的 Perl 内部结构。
Perl 文件处理很重要,因为它有助于访问文件,例如文本文件、日志文件或配置文件。
Perl 文件句柄能够创建、读取、打开和关闭文件。

Perl 创建文件

我们在 open() 函数的帮助下创建一个文件 file1.txt。
$fh(文件句柄)是一个标量变量,我们可以在 open() 函数内部或之前定义它。这里我们在函数内部定义了它。 '>' 符号表示我们正在打开此文件进行写入。 $filename 表示路径或文件位置。
打开文件后,在打印语句中使用 $fh。 print() 函数将在文件中打印上述文本。
现在我们关闭 $fh。好吧,在 perl 中不需要关闭文件。当变量超出范围时,您的文件将自动关闭。
my $filename = 'file1.txt';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
print$fh "Hello!! We have created this file as an example\n";
close $fh;
print"done\n";
输出:
done.
在我们的系统中将创建一个文件 file1.txt。

Perl 打开文件

我们可以通过以下方式打开文件:
(<) 语法
<符号用于打开一个已经存在的文件。它以读取模式打开文件。< div>
open FILE, "<", "fileName.txt" or die $!
(>) 语法
> 符号用于打开和创建不存在的文件。它以写入模式打开文件。
open FILE, ">", "fileName.txt" or die $!
"<"符号将在打开文件之前清空文件。它将清除该文件的所有数据。为防止出现这种情况,请在 ">" 或 "<" 字符前使用(+) 符号。
(+>+<) 语法
open FILE, "+<", "fileName.txt" or die $!
open FILE, "+>", "fileName.txt" or die $!
(>>) 语法
>> 符号用于读取和附加文件内容。它将文件指针放在文件末尾,您可以在其中附加信息。在这里,要从这个文件中读取,你需要在">>"符号前加上(+) 符号。
open FILE, "<", "fileName.txt" or die $!

Perl 读取文件

您可以一次读取一个完整的文件,也可以一次读取一行。我们将为两者展示一个示例。打开文件进行读取类似于打开文件进行写入。唯一的区别是">"用于写入,"<"用于读取文件。
我们创建了一个文件file1.txt,其内容如下:
this is the First Line.
this is the Second Line.
this is the Third Line.
this is the Fourth Line.

一次读取一行

将显示file1.txt的第一行。 $row 的内容将打印"done",以表明我们在程序结束时到达。
use strict;
use warnings;
my $filename = 'file1.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)
  or die "Could not open file '$filename' $!";
my $row = <$fh>;
print"$row\n";
print"done\n";
输出:
this is the First Line.
Done.

一次读取多行

现在我们知道从文件中读取单行。要读取多行,将 $row 放入 while 循环中。
每次,当 while 循环达到其条件时,它会执行 my $row = <$fh>。它将从文件中读取下一行。在最后一行,$fh 将返回 undef,这是 false 并且循环将终止。
use strict;
use warnings;
my $filename = 'file1.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)
  or die "Could not open file '$filename' $!";
while (my $row = <$fh>) {
  chomp $row;
  print"$row\n";
}
print"done\n";
输出:
this is the First Line.
this is the Second Line.
this is the Third Line.
this is the Fourth Line.
Done.

Perl 写入文件

通过文件写入,我们将在 file1.txt 中追加行。如前所述,将在文件的最后添加新行。
open (FILE, ">> file1.txt") || die "problem opening $file1.txt\n";
printFILE "this line is added in the file1.txt\n";
# FILE array of lines is written here
printFILE @lines1;
# Another FILE array of lines is written here
printFILE "A complete new file is created";
# write a second array of lines to the file
printFILE @lines2;
输出:
this line is added in the file1.txt
A complete new file is created

Perl 关闭文件

Perl 关闭文件用于使用 close() 函数关闭文件句柄。在 perl 中文件关闭不是强制性的。一旦变量超出范围,Perl 会自动关闭文件。
open FILE1, "file1.txt" or die $!;
...
close FILE1;

Perl 文件句柄运算符,

文件句柄运算符是从文件中读取信息的主要方法。它用于获取用户的输入。在标量上下文中,它从文件句柄中返回一行,在行上下文中,它从文件句柄中返回一个行列表。
print"What is your age?\n";
$age = <STDIN>;
if($age >= 18)
{
   print"You are eligible to vote.\n";
} else {
   print"You are not eligible to vote.\n";
    }
输出:
1. What is your age?
      18
      You are eligible to vote
2.  What is your age?
      16
       You are not eligible to vote.

Perl 文件句柄 print() 函数

print() 函数打印回通过文件句柄运算符给出的信息。
print"Welcome to my site\n";
输出:
Welcome to my site  

Perl 复制文件

我们可以将一个文件的内容原样复制到另一个文件中。先打开file1,再打开file2、通过 while 循环读取文件 1 的行,将文件 1 的内容复制到文件 2、
# Opening file1 to read
open(File1Data, "<file1.txt");
# Opening new file to copy content of file1
open(File2Data, ">file2.txt");
# Copying data from file1 to file2.
while(<File1Data>)
{
   printFile2Data $_;
}
close( File1Data );
close( File2Data );
输出:
done
将在 file1.pl 所在位置创建一个新文件 file2.pl。

Perl 文件测试操作符

有不同的测试操作符可以检查文件的不同信息。部分测试操作符如下:
测试操作员 说明
-A 返回自程序启动以来文件的总访问时间。
-b 检查文件是否为块设备。
-B 检查是否是二进制文件。
-c 检查文件是否为字符设备
-C 返回自程序启动以来文件的 inode 更改时间。
-d 检查文件是否为目录。
-e 检查文件是否存在。
-f 检查文件类型是普通文件、符号链接还是其他类型的文件。
-g 检查文件是否设置了 setgid 位。
-k 检查文件是否设置了粘滞位。
-l 检查文件是否为符号链接。在dos中,它总是返回false。
-M 以天为单位返回程序启动后的文件修改时间。
-o 检查文件是否由有效的uid拥有,在dos中,它总是返回true。
-O 检查文件是否由真实的uid拥有,在dos中,它总是返回true。
-p 检查文件是否为命名管道
-r 检查文件是否可读。
-R 检查文件是否可以被真实的 uid 读取。在dos中,与-r相同。
-s 以字节为单位返回文件大小。
-S 检查文件是否为套接字。
-t 检查文件是否打开到 tty(终端)
-T 检查文件是否为文本文件。
-u 检查文件是否设置了 setuid 位。
-w 检查文件是否可写。
-W 检查文件是否可由真实的 uid/gid 写入。
-x 检查文件是否可以执行。
-X 检查文件是否可以被真实的 uid/gid 执行
-z 检查文件大小是否为零。

Perl 使用文件测试操作符

为了测试 Perl 中的不同功能,提供了一系列测试操作符。在给定的示例中,我们测试了 file1.txt 的不同功能。所有结果都用 join() 函数合并。
my $a = "/Users/lidihuo/Desktop/file1.txt";
my (@description, $size);
if (-e $a)
{
   push @description, 'binary' if (-B _);
   push @description, 'a socket' if (-S _);
   push @description, 'a text file' if (-T _);
   push @description, 'a block special file' if (-b _);
   push @description, 'a character special file' if (-c _);
   push @description, 'a directory' if (-d _);
   push @description, 'executable' if (-x _);
   push @description, (($size =-s _)) ? "$size bytes" : 'empty';
   print"file1.txt is ", join(', ',@description),"\n";
}
输出:
file1.txt is a text file, 67 bytes
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4