个性化阅读
专注于IT技术分析

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打开文件

我们可以通过以下方式打开文件:

(<)语法

<符号用于打开一个已经存在的文件。它以读取模式打开文件。

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循环达到其条件时, 它将执行我的$ 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";
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关闭文件

Perl关闭文件用于使用close()函数关闭文件句柄。在perl中文件关闭不是强制性的。一旦变量超出范围, Perl就会自动关闭文件。

open FILE1, "file1.txt" or die $!;
...
close FILE1;

Perl文件句柄运算符, <FILEHANDL>

文件句柄运算符是从文件读取信息的主要方法。它用于获取用户的输入。在标量上下文中, 它从文件句柄返回一行, 在行上下文中, 它从文件句柄返回一行列表。

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>)
{
   print File2Data $_;
}
close( File1Data );
close( File2Data );

输出

done

将在file1.pl所在的位置创建一个新文件file2.pl。


Perl文件测试操作员

有不同的测试操作员来检查有关文件的不同信息。一些测试运算符如下:

Test operators Description
-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/srcmini/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
赞(0)
未经允许不得转载:srcmini » Perl文件处理用法和实例

评论 抢沙发

评论前必须登录!