perl幾個文件操作例子
perl用的最多的地方就算是文件處理了,下面我就總結(jié)了一下perl文件操作的一些東西,并且有具體的例子,通過下面的例子,加強我們對perl文件操作的理解。
刪除文件
使用unlinke函數(shù),比如unlink $file, unlink $file1, $file2, $file3
打開文件
使用三參數(shù)的形式打開文件,這樣非常便于區(qū)分模式和文件名,perl 5.6之后的版本都支持這種方式。
#Open the 'txt' file for reading
open FH, '<', "$file_name.txt" or die "Error:$!n"; #Open the 'txt' file for writing. Creates the #file_name if it doesn't already exist #and will delete/overwrite a pre-existing file of the same name open FH, '>', "$file_name.txt" or die "Error:$!n";
#Open the 'txt' file for appending. Creates the #file_name if it doesn't already exist
open FH, '>>', "$file_name.txt" or die "Error:$!n";
#Open the 'txt' file for a 'read/write'. #Will not create the file if it doesn't #already exist and will not delete/overwrite #a pre-existing file of the same name
open FH, '+<', "$file_name.txt" or die "Error:$!n"; #Open the 'txt' file for a 'read/write'. Will create #the file if it doesn't already exist and will #delete/overwrite a pre-existing file #of the same name open FH, '+>', "$file_name.txt" or die "Error:$!n";
#Open the 'txt' file for a 'read/append'. Will create #the file if it doesn't already exist and will #not delete/overwrite a pre-existing file #of the same name
open FH, '+>>', "$file_name.txt" or die "Error:$!n";
一次性讀入整個文件
使用<>在標量環(huán)境下一次讀入一行,而在列表環(huán)境下一次讀入所有行,$/存儲的是行分隔符,默認是換行符,我們先將$/改掉,這樣就可 以在標量環(huán)境下一次讀入所有行了(這時已經(jīng)沒有行的概念了,就是讀入整個文件),你也可以用列表讀入所有行然后再將所有行拼到一起,但那樣速度很慢。用完記得將$/改回來。
#!/usr/bin/perl
use strict ;
use warnings ;
sub test{
open FILE, '<', "d:/code/test.txt" or die $! ;
my $olds = $/ ;
$/ = undef ;
my $slurp = ;
print $slurp, "n" ;
$/ = $olds ;
close FILE;
}
&test() ;
也可以使用local關(guān)鍵字來將$/設(shè)置為局部變量,這樣跳出作用域后,$/又恢復(fù)了原來的值。
#!/usr/bin/perl
use strict ;
use warnings ;
sub test{
local $/ ; #??? local $/ = undef ;
open FILE, '<', "d:/code/zdd.txt" or die $! ;
my $slurp = ;
print $slurp, "n" ;
}
&test() ;
最好的方法是使用模塊,這樣比自己寫安全,F(xiàn)ile::Slurp、IO::All都可以的。
打開文件請用雙引號
open文件時,如果文件名有變量替換,最好用雙引號而不是單引號,因為單引號無視變量內(nèi)插。
open FILE "<$file" or die $! ; #這樣可以。
open FILE '<$file' or die $! ; #這樣就不可以,因為$file不會被解釋成變量內(nèi)插。同樣<也不會被解釋成輸入
文件句柄作參數(shù)
假設(shè)有一個函數(shù)test,它有一個參數(shù),是某個文件句柄,那么該如何傳遞這個參數(shù)呢?
方法一,傳遞參數(shù)時,在句柄前面加*
sub main {
open FILE, '+<', 'test.data' or die $!;
&test(*FILE);
close FILE;
}
方法二,使用open my $FILE的形式打開文件
sub main {
open my $FILE, '+<', 'test.data' or die $!;
&test($FILE);
close $FILE;
}
相關(guān)文章
使用perl實現(xiàn)拆分數(shù)據(jù)表(mysql)并遷移數(shù)據(jù)實例
這篇文章主要介紹了使用perl實現(xiàn)拆分數(shù)據(jù)表(mysql)并遷移數(shù)據(jù)實例,本文提供了3個腳本,分別用于拆分數(shù)據(jù)表、遷移數(shù)據(jù)、插入測試數(shù)據(jù),需要的朋友可以參考下2014-10-10使用perl清理電腦上重復(fù)文件實現(xiàn)代碼(續(xù))
使有perl搜索電腦上的重復(fù)文件并刪除,需要的朋友可以參考下2013-02-02學(xué)習(xí)perl的unless控制結(jié)構(gòu)
在perl的if控制結(jié)構(gòu)中,只有當(dāng)條件表達式為真時才執(zhí)行某塊代碼。如果想讓程序塊在條件為假時才執(zhí)行,此時可以把if改成unless2013-02-02perl的格式化(Format)報表輸出實現(xiàn)代碼
perl有最好的文本數(shù)據(jù)處理能力.這是大家都知道的.在perl本身有一個別的軟件沒有的小功能,就是Perl格式.它相當(dāng)于簡單的命令行報表和圖表輸出2013-01-01兩段Perl腳本代碼(數(shù)組應(yīng)用與say用法)
兩段Perl代碼(數(shù)組應(yīng)用與say用法),供大家學(xué)習(xí)參考2013-02-02