茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    perl幾個(gè)文件操作例子
    來源:易賢網(wǎng) 閱讀:1083 次 日期:2016-06-30 09:18:35
    溫馨提示:易賢網(wǎng)小編為您整理了“perl幾個(gè)文件操作例子”,方便廣大網(wǎng)友查閱!

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

    刪除文件

    使用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;

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

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

    代碼如下:

    #!/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ù)了原來的值。

    代碼如下:

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

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

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

    代碼如下:

    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í),在句柄前面加*

    代碼如下:

    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;

    }

    更多信息請(qǐng)查看腳本欄目
    易賢網(wǎng)手機(jī)網(wǎng)站地址:perl幾個(gè)文件操作例子
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026國(guó)考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)