婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > perl文件操作的一些例子分享

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

熱門標簽:買了外呼系統(tǒng)不想用了怎么辦 真人語音電銷機器人系統(tǒng) 電話機器人電話卡封號怎么辦 浦東上海400開頭的電話申請 邯鄲外呼調(diào)研線路 開封百應電銷機器人聯(lián)系方式 樂昌電話機器人 北京語音電銷機器人價格 武漢呼叫中心外呼系統(tǒng)線路商

刪除文件

使用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 = FILE> ;
    print $slurp, "\n" ;
    $/ = $olds ;
    close FILE;
}
test() ;

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

復制代碼 代碼如下:

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

打開文件請用雙引號

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

復制代碼 代碼如下:

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

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

標簽:淄博 河北 鄂州 松原 六安 石嘴山 宜春 自貢

巨人網(wǎng)絡通訊聲明:本文標題《perl文件操作的一些例子分享》,本文關(guān)鍵詞  perl,文件,操作,的,一些,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《perl文件操作的一些例子分享》相關(guān)的同類信息!
  • 本頁收集關(guān)于perl文件操作的一些例子分享的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 清丰县| 波密县| 广东省| 吉安市| 桓台县| 无极县| 车致| 蓬莱市| 冷水江市| 随州市| 黄梅县| 云林县| 石棉县| 远安县| 碌曲县| 榕江县| 尚义县| 长葛市| 马龙县| 吉隆县| 西吉县| 永仁县| 灵宝市| 教育| 佳木斯市| 七台河市| 龙岩市| 顺昌县| 定西市| 图木舒克市| 宝清县| 永修县| 大足县| 济南市| 锡林郭勒盟| 台前县| 济源市| 亚东县| 贡嘎县| 色达县| 罗定市|