最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

perl文件操作的一些例子分享

 更新時(shí)間:2013年02月05日 14:28:55   作者:  
有關(guān)perl文件操作的一些例子,供大家學(xué)習(xí)參考

刪除文件

使用unlinke函數(shù),比如unlink $file, unlink $file1, $file2, $file3

打開文件

使用三參數(shù)的形式打開文件,這樣非常便于區(qū)分模式和文件名,perl 5.6之后的版本都支持這種方式。

復(fù)制代碼 代碼如下:

#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";

一次性讀入整個(gè)文件

使用<>在標(biāo)量環(huán)境下一次讀入一行,而在列表環(huán)境下一次讀入所有行,$/存儲(chǔ)的是行分隔符,默認(rèn)是換行符,我們先將$/改掉,這樣就可 以在標(biāo)量環(huán)境下一次讀入所有行了(這時(shí)已經(jīng)沒(méi)有行的概念了,就是讀入整個(gè)文件),你也可以用列表讀入所有行然后再將所有行拼到一起,但那樣速度很慢。用完 記得將$/改回來(lái)。

復(fù)制代碼 代碼如下:

#!/usr/bin/perl
use strict ;
use warnings ;
sub test{
    open FILE, '<', "d:/code/test.txt" or die $! ;
    my $olds = $/ ;
    $/ = undef ;
    my $slurp = <FILE> ;
    print $slurp, "\n" ;
    $/ = $olds ;
    close FILE;
}
&test() ;

也可以使用local關(guān)鍵字來(lái)將$/設(shè)置為局部變量,這樣跳出作用域后,$/又恢復(fù)了原來(lái)的值。

復(fù)制代碼 代碼如下:

#!/usr/bin/perl
use strict ;
use warnings ;
sub test{
    local $/ ; #??? local $/ = undef ;
    open FILE, '<', "d:/code/zdd.txt" or die $! ;
    my $slurp = <FILE> ;
    print $slurp, "\n" ;
}
&test() ;
1;

最好的方法是使用模塊,這樣比自己寫安全,F(xiàn)ile::Slurp、IO::All都可以的。

打開文件請(qǐng)用雙引號(hào)

open文件時(shí),如果文件名有變量替換,最好用雙引號(hào)而不是單引號(hào),因?yàn)閱我?hào)無(wú)視變量?jī)?nèi)插。

復(fù)制代碼 代碼如下:

open FILE "<$file" or die $! ; #這樣可以。
open FILE '<$file' or die $! ; #這樣就不可以,因?yàn)?file不會(huì)被解釋成變量?jī)?nèi)插。同樣<也不會(huì)被解釋成輸入符號(hào)。

文件句柄作參數(shù)

假設(shè)有一個(gè)函數(shù)test,它有一個(gè)參數(shù),是某個(gè)文件句柄,那么該如何傳遞這個(gè)參數(shù)呢?

方法一,傳遞參數(shù)時(shí),在句柄前面加*

復(fù)制代碼 代碼如下:

sub main {
    open FILE, '+<', 'test.data' or die $!;
    &test(*FILE);
    close FILE;
}

方法二,使用open my $FILE的形式打開文件

復(fù)制代碼 代碼如下:

sub main {
    open my $FILE, '+<', 'test.data' or die $!;
    &test($FILE);
    close $FILE;
}

相關(guān)文章

最新評(píng)論

夏津县| 房山区| 轮台县| 汝阳县| 台北市| 怀远县| 旬阳县| 林口县| 荆门市| 泸西县| 墨江| 嘉兴市| 攀枝花市| 通州区| 新竹市| 双柏县| 周至县| 吉安市| 民权县| 永州市| 普格县| 简阳市| 嘉禾县| 象山县| 喀喇沁旗| 万年县| 永春县| 华安县| 广宗县| 太原市| 清河县| 通许县| 达拉特旗| 启东市| 恭城| 乌兰县| 德昌县| 宜春市| 弥渡县| 苍南县| 大新县|