在本文中,让我们讨论如何在Perl中操作文件处理程序。
1.打开Perl文件处理程序的典型方法
下面的perl示例打开了一个带有裸词的文件。这是一个典型的perl文件打开方案。
#!/usr/bin/perl open FH,"</tmp/msg";
读取带有Bareword文件句柄的操作:
#!/usr/bin/perl open FH,"</tmp/msg"; $line = <FH>; print $line;
使用Bareword文件句柄进行写操作:
#!/usr/bin/perl open FH,">/tmp/msg"; print FH "Perl - Practical Extraction Report Language\n";
如果要将此处理程序传递给perl函数,则可以使用typeglob,如下所示。
#!/usr/bin/perl open FH,"</tmp/msg"; read_text(*FH); sub read_text { local *FH = shift; my @lines; @lines = <FH>; print @lines; }
2.在普通标量变量中打开一个Perl文件句柄引用
您可以使用标量变量来存储文件句柄引用,如下所示。
#!/usr/bin/perl # $log_fh declared to store the file handle. my $log_fh; open $log_fh,"</tmp/msg"; read_text($log_fh); sub read_text { local $log_fh = shift; my @lines; @lines = <$log_fh>; print @lines; }
3.使用Perl IO :: File打开文件句柄
IO :: File是perl标准的CPAN模块,该模块用于以其他五颜六色的约定打开文件句柄。使用 cpan命令 安装perl模块。
#!/usr/bin/perl use IO::File; $read_fh = IO::File->new("/tmp/msg",'r'); read_text($read_fh); sub read_text { local $read_fh = shift; my @lines; @lines = <$read_fh>; print @lines; }
以下perl代码片段说明了IO :: File模块的perl写操作。
$write_fh = IO::File->new("/tmp/msg",'w');
要以追加模式打开文件处理程序,请执行以下操作。
$fh = IO::File->new("/tmp/msg",O_WRONLY|O_APPEND);
4.以读写模式打开Perl文件处理程序
当您想同时以读写模式打开时,Perl允许您这样做。下面的perl模式符号用于在相应模式下打开文件句柄。
模式 | 描述 |
---|---|
+< | 读,写 |
+> | 读,写,截断,创建 |
+>> | 读取,写入,创建,添加 |
让我们编写一个perl程序示例,以便以读写模式打开示例文本文件。
$ cat / tmp / text one two three four five
The below code reads first line from the / tmp / text file and immediately does the write operation.
#!/usr/bin/perl open(FH,"+</tmp/text"); read_line(*FH); write_line(*FH,"222\n"); sub read_line { local *FH = shift; my $lines; $line = <FH>; print $line; } sub write_line { local *FH = shift; print FH @_; } close(FH);
上面代码的输出如下所示。
$ perl ./read_and_write.pl one $ cat / tmp / text one 222 three four five
注意: 使用 Perl调试器 调试perl脚本。
5.打开标准输入和标准输出
Perl允许您使用其他文件句柄名称打开标准输入和标准输出。
Perl标准输出示例:
#!/usr/bin/perl open(OUT,">-"); print OUT "STDOUT opened with the name as OUT";
Perl标准输入示例:
#!/usr/bin/perl open(IN,"-"); print "STDIN opened with the name as IN"; $input = <IN>;
6.使用sysopen()打开文件
sysopen()函数需要三个参数,例如文件句柄,文件名和模式。
读取操作示例:
#!/usr/bin/perl sysopen(FH,"/ tmp / text",O_RDONLY); $line = <FH>; print $line;
写操作示例:
#!/usr/bin/perl sysopen(FH,"/ tmp / text",O_WRONLY); print FH "write operation";
下表显示了不同类型的模式。
模式 | 描述 |
---|---|
O_RDONLY | 读 |
O_WRONLY | 写 |
O_RDWR | 读和写 |
O_CREAT | 创建 |
O_APPEND | 附加 |
O_TRUNC | 截短 |
O_NONBLOCK | 非块模式 |
注意 : 您需要养成验证打开的文件处理程序的习惯。下面显示了使用die函数处理文件处理程序打开失败的最常见方法。
open(FH,">/tmp/text") or die "Could not open / tmp / text file : $!\n";
如果以上代码无法打开文件“/tmp/text”,它返回失败,并执行死。和“$!”Buildin变量包含打开功能失败的原因。
如果您喜欢这篇文章,您可能还会喜欢..
![]() |
![]() |
![]() |
![]() |
我有一个这样的csv文件:
TranID,Date,AcNo,Type,Amount,ChequeNo,DDNo,Bank,Branch
132520,01-01-2011,51321342,博士,5000 ,,,,
132524,01-01-2011,51321342,Dr,1000,,4126123,SB,Ashoknagar
132538,08-01-2011,51321342,Cr,1620,192101 ,,,
132548,17-01-2011,51321342,Cr,3500,192102 ,,,
132519,01-01-2011,55212341,Dr,2000,142514,,SBM,Hampankatte
132523,01-01-2011,55212341,Cr,500,192121 ,,,
132529,02-01-2011,55212341,Dr,5000,131211,,SB,Ashoknagar
132539,09-01-2011,55212341,Cr,500,192122 ,,,
132541,10-01-2011,55212341,Cr,2000,192123 ,,,
132525,02-01-2011,55212342,Dr,5000 ,,,,
132533,04-01-2011,55212342,Cr,2100,192201 ,,,
132526,02-01-2011,55212343,Dr,3000,126200,,ICICI,Hampankatte
132531,03-01-2011,55212343,Cr,500,192221 ,,,
132537,07-01-2011,55212343,Dr,5456,135123,,CB,Kankanady
132544,15-01-2011,55212343,Cr,3500,192222 ,,,
132546,15-01-2011,55212343,Cr,1342,192223 ,,,
132527,02-01-2011,55212344,Dr,5000,127821,,SB,MGRoad
132536,06-01-2011,55212344,Cr,1500,192251 ,,,
132528,02-01-2011,55212345,Dr,2000,162423,,CB,MGRoad
132530,03-01-2011,55212345,Cr,1000,192271 ,,,
132542,11-01-2011,55212345,Dr,3500,,4251234,ICICI,Hampankatte
132543,14-01-2011,55212345,Cr,1500,192272 ,,,
132521,01-01-2011,55212346,Dr,6000,125324,,CB,Kankanady
132532,03-01-2011,55212346,Cr,1000,192341 ,,,
132547,16-01-2011,55212346,Dr,2300 ,,,,
132522,01-01-2011,55212415,Dr,1200,162341,,SBI,Hampankatte
132534,05-01-2011,55212415,Dr,5000,162450,,SB,Kuloor
132535,06-01-2011,55212415,Cr,2000,192361 ,,,
132540,09-01-2011,55212415,Dr,2000,4521349,CB,MGRoad
132545,15-01-2011,55212415,Cr,1245,192362 ,,,
如何在perl中使用哈希计算和显示每个帐户的总余额。不使用解析功能