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

首頁 > 編程 > Perl > 正文

perl幾個文件操作例子

2020-06-04 20:31:49
字體:
來源:轉載
供稿:網友

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都可以的。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一本色道久久99精品综合蜜臀 | 可以看逼的视频 | 一区二区三区日本在线观看 | 成人午夜免费福利 | 新久久久久久 | 成人资源在线观看 | 欧美日韩一区二区综合 | 成人在线观看地址 | 羞羞电影在线观看www | 国产精品视频 | 国产毛片网 | 久久久久免费电影 | 久久国产成人午夜av浪潮 | 欧美片a | 欧美日韩免费观看视频 | 国内精品免费一区二区2001 | av日韩在线免费观看 | 在线视频 亚洲 | 欧日一级片 | 久久久aa| 艹男人的日日夜夜 | 久热久操 | 精品国产一区二区三区久久久蜜月 | 91在线视频网址 | 欧美精品成人一区二区在线观看 | 国产精品91久久久 | 久久久久在线观看 | 国产在线精品一区二区夜色 | 日本aaaa片毛片免费观蜜桃 | 久久久久电影网站 | av在线看网站 | av电影在线免费 | 宅男噜噜噜66国产免费观看 | 国产美女爽到喷白浆的 | xxxx69hd一hd72 | 精品国产乱码久久久久久久 | 日韩精品一区二区三区中文 | 看91| 一级黄色国产视频 | 精品成人av一区二区三区 | 姑娘第5集高清在线观看 |