正文

Perl学习笔记之文件操作(perl常用函数)一篇读懂

文章摘要

这篇文章介绍了一种使用 Perl 语言实现的文件读取程序。程序通过 `open` 函数打开指定的文件 `test.txt`,并使用 `while` 循环逐行读取文件内容。如果文件无法打开,则显示错误信息并退出。代码中包含了基本的文件操作流程和错误处理机制,适合初学者学习和参考。

 #! c:/perl/bin/perl -w use utf8; use strict; use warnings; my $filename=’test.txt’; if(open(MYFILE,$filename)) { my @myfile=<MYFILE>; #如果要读取多行,用此方法,如果只读取一行为:$myfile=<>; my $count=0; #要读取的行数,初始值为0 printf “I have opened this file: %s\n”, $filename; while($count < @myfile){ #遍历 print (“$myfile[$count]\n”); #注意此种写法. $count++; } close(MYFILE); } else{ print “I can’t open this file!”; } exit;