a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > Perl > 正文

perl幾個文件操作例子

2020-10-31 15:16:37
字體:
來源:轉載
供稿:網友

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

刪除文件

使用unlinke函數,比如unlink $file, unlink $file1, $file2, $file3

打開文件

使用三參數的形式打開文件,這樣非常便于區分模式和文件名,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";

一次性讀入整個文件

使用<>在標量環境下一次讀入一行,而在列表環境下一次讀入所有行,$/存儲的是行分隔符,默認是換行符,我們先將$/改掉,這樣就可 以在標量環境下一次讀入所有行了(這時已經沒有行的概念了,就是讀入整個文件),你也可以用列表讀入所有行然后再將所有行拼到一起,但那樣速度很慢。用完記得將$/改回來。

復制代碼 代碼如下:

#!/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關鍵字來將$/設置為局部變量,這樣跳出作用域后,$/又恢復了原來的值。
復制代碼 代碼如下:

#!/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() ;

最好的方法是使用模塊,這樣比自己寫安全,File::Slurp、IO::All都可以的。

打開文件請用雙引號

open文件時,如果文件名有變量替換,最好用雙引號而不是單引號,因為單引號無視變量內插。

復制代碼 代碼如下:

open FILE "<$file" or die $! ; #這樣可以。
open FILE '<$file' or die $! ; #這樣就不可以,因為$file不會被解釋成變量內插。同樣<也不會被解釋成輸入

文件句柄作參數

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

方法一,傳遞參數時,在句柄前面加*

復制代碼 代碼如下:

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;
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品精品视频一区二区三区 | 欧美日韩久久精品 | 欧美激情综合色综合啪啪五月 | 美女中文字幕视频 | 国产一区二区精品丝袜 | 农村妇女毛片精品久久久 | 黄页网站免费在线观看 | 一区二区视频网站 | 精品亚洲一区二区三区四区五区 | 成人精品一区二区三区中文字幕 | 亚洲精品视频在线播放 | 四虎影城 | 国外成人在线视频网站 | 视频一区久久 | 日韩综合一区 | 日本久久久久 | 国产 高清 在线 | 精品视频免费 | 天堂一区二区三区四区 | 特级淫片女子高清视频在线观看 | 得得啪在线视频 | 亚洲视频在线观看网站 | 亚洲欧洲一区二区 | 国产精品视频免费 | 色爱区综合 | 欧美成人精品一区二区男人看 | www.日韩av.com | 精品国产污污免费网站入口 | 一本色道精品久久一区二区三区 | 国内自拍网站 | 一区二区三区在线播放 | 欧美日韩中字 | 一级免费片 | 激情.com | 一区免费视频 | 日本在线观看www | 日韩一区二区三区在线 | 亚洲午夜精品一区二区三区 | 亚洲精品亚洲人成人网 | 久久久久久久久一区二区 | 日本一本在线 |