麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Perl > 正文

perl幾個(gè)文件操作例子

2020-10-31 15:16:37
字體:
供稿:網(wǎng)友

perl用的最多的地方就算是文件處理了,下面我就總結(jié)了一下perl文件操作的一些東西,并且有具體的例子,通過下面的例子,加強(qiáng)我們對(duì)perl文件操作的理解。

刪除文件

使用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)沒有行的概念了,就是讀入整個(gè)文件),你也可以用列表讀入所有行然后再將所有行拼到一起,但那樣速度很慢。用完記得將$/改回來。

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

#!/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ù)了原來的值。
復(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都可以的。

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

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

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

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

文件句柄作參數(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;
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 成人在线视频免费观看 | 精品久久久久久国产 | 久久久久久久久久久久免费 | 国产无遮挡一区二区三区毛片日本 | 精品一区二区久久久久 | 人人玩人人爽 | 久久精品视频69 | 热久久成人 | 久久久久久久久浪潮精品 | 成人精品 | 成人做爰高潮片免费视频韩国 | 91在线色| 337p日本欧洲亚洲大胆精蜜臀 | 91看片王| 色吧久久 | 欧美成年人在线视频 | 亚洲午夜免费电影 | 久久免费综合视频 | 欧美一区二区黄 | aa国产视频一区二区 | 中文区永久区 | 麻豆91精品91久久久 | 中文字幕免费看 | 日韩午夜片 | 超碰一区 | 嫩草www| 91av大片 | 成人三级视频网站 | 亚洲第五色综合网 | 国产精品爆操 | 久久久一区二区三区视频 | 黄色网址免费在线播放 | 久久久久免费电影 | 久久av免费| 中文字幕在线观看精品 | 成人男女激情免费视频 | 午夜视频成人 | 草草在线观看 | 亚洲天堂成人在线 | 在线观看国产日韩 | 成人精品久久久 |