📅  最后修改于: 2021-01-07 08:35:12             🧑  作者: Mango
文件处理是任何编程语言中最重要的部分。文件句柄是与文件名关联的内部Perl结构。
Perl文件处理很重要,因为它有助于访问文件,例如文本文件,日志文件或配置文件。
Perl文件句柄能够创建,读取,打开和关闭文件。
我们正在借助open()函数创建文件file1.txt 。
$ fh(文件句柄)是一个标量变量,我们可以在open()函数内部或之前定义它。在这里,我们在函数内部定义了它。 “>”符号表示我们正在打开该文件进行写入。 $ filename表示路径或文件位置。
打开文件后,在print语句中使用$ fh。 print()函数将在文件中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.
我们可以通过以下方式打开文件:
(<)语法
<符号用于打开一个已经存在的文件。它以读取模式打开文件。
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 $!
您可以一次读取一个完整的文件,也可以一次读取一行。我们将展示两个示例。打开要读取的文件类似于打开要写入的文件。仅使用“>”写入和使用“ <”读取文件有一个区别。
我们创建了一个文件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循环达到其条件时,它将执行我的$ 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.
通过文件写入,我们将在file1.txt中添加行。如前所述,新行将添加到文件的最后。
open (FILE, ">> file1.txt") || die "problem opening $file1.txt\n";
print FILE "This line is added in the file1.txt\n";
# FILE array of lines is written here
print FILE @lines1;
# Another FILE array of lines is written here
print FILE "A complete new file is created";
# write a second array of lines to the file
print FILE @lines2;
输出:
This line is added in the file1.txt
A complete new file is created
Perl关闭文件用于使用close()函数关闭文件句柄。在perl中,文件关闭不是强制性的。一旦变量超出范围,Perl就会自动关闭文件。
open FILE1, "file1.txt" or die $!;
...
close FILE1;
文件句柄运算符是从文件读取信息的主要方法。它用于获取用户的输入。在标量上下文中,它从文件句柄返回一行,而在行上下文中,它从文件句柄返回一行列表。
print "What is your age?\n";
$age = ;
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.
print()函数打印回通过文件句柄运算符给出的信息。
print "Welcome to my site\n";
输出:
Welcome to my site
我们可以将一个文件的内容照原样复制到另一个文件中。首先打开file1,然后打开file2。通过while循环读取文件1的内容,将文件1的内容复制到文件2。
# Opening file1 to read
open(File1Data, "file2.txt");
# Copying data from file1 to file2.
while()
{
print File2Data $_;
}
close( File1Data );
close( File2Data );
输出:
done
将在file1.pl存在的位置创建一个新文件file2.pl。
有不同的测试运算符来检查有关文件的不同信息。一些测试运算符如下:
Test operators | Description |
-A | Return total access time of file since the program started. |
-b | Check whether file is block device or not. |
-B | Check whether it is a binary file. |
-c | Check whether file is character device. |
-C | Return inode change time of file since the program started. |
-d | Check whether file is a directory. |
-e | Check whether file exists or not. |
-f | Check type of file whether it is regular, symbolic link or other type of file. |
-g | Check whether file have setgid bit set. |
-k | Check whether file have sticky bit set. |
-l | Check if file is a symbolic link. In dos, it always return false. |
-M | Return file modification time in days since the program started. |
-o | Check if file is owned by an effective uid, in dos, it always return true. |
-O | Check if file is owned by a real uid, in dos, it always return true. |
-p | Check whether file is a named pipe. |
-r | Check whether file is readable or not. |
-R | Check whether file is readable by real uid or not. In dos, it is same as -r. |
-s | Return the file size in bytes. |
-S | Check if file is a socket. |
-t | Check if file is opened to a tty (terminal) |
-T | Check if file is a text file. |
-u | Check if file has setuid bit set. |
-w | Check if file is writable or not. |
-W | Check if file is writable by real uid/gid. |
-x | Check if file can be executed or not. |
-X | Check if file can be executed by real uid/gid. |
-z | Check if file size is zero or not. |
为了测试Perl中的不同功能,提供了一系列测试运算符。在给定的示例中,我们测试了file1.txt的不同功能。所有结果都与join()函数合并。
my $a = "/Users/javatpoint/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