精品免费在线观看-精品欧美-精品欧美成人bd高清在线观看-精品欧美高清不卡在线-精品欧美日韩一区二区

17站長網(wǎng)

17站長網(wǎng) 首頁 腳本 perl 查看內(nèi)容

perl文件操作的一些例子

2022-12-8 15:33| 查看: 2410 |來源: 互聯(lián)網(wǎng)

刪除文件 使用unlinke函數(shù),比如unlink $file, unlink $file1, $file2, $file3 打開文件 使用三參數(shù)的形式打開文件,這樣非常便于區(qū)分模式和文件名,perl 5.6之后的版本都支持這種方式。#Open the 'txt' file fo

刪除文件

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

一次性讀入整個文件

使用<>在標(biāo)量環(huán)境下一次讀入一行,而在列表環(huán)境下一次讀入所有行,$/存儲的是行分隔符,默認是換行符,我們先將$/改掉,這樣就可 以在標(biāo)量環(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 = ;
    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() ;
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è)有一個函數(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;
}

本文最后更新于 2022-12-8 15:33,某些文章具有時效性,若有錯誤或已失效,請在網(wǎng)站留言或聯(lián)系站長:[email protected]
·END·
站長網(wǎng)微信號:w17tui,關(guān)注站長、創(chuàng)業(yè)、關(guān)注互聯(lián)網(wǎng)人 - 互聯(lián)網(wǎng)創(chuàng)業(yè)者營銷服務(wù)中心

免責(zé)聲明:本站部分文章和圖片均來自用戶投稿和網(wǎng)絡(luò)收集,旨在傳播知識,文章和圖片版權(quán)歸原作者及原出處所有,僅供學(xué)習(xí)與參考,請勿用于商業(yè)用途,如果損害了您的權(quán)利,請聯(lián)系我們及時修正或刪除。謝謝!

17站長網(wǎng)微信二維碼

始終以前瞻性的眼光聚焦站長、創(chuàng)業(yè)、互聯(lián)網(wǎng)等領(lǐng)域,為您提供最新最全的互聯(lián)網(wǎng)資訊,幫助站長轉(zhuǎn)型升級,為互聯(lián)網(wǎng)創(chuàng)業(yè)者提供更加優(yōu)質(zhì)的創(chuàng)業(yè)信息和品牌營銷服務(wù),與站長一起進步!讓互聯(lián)網(wǎng)創(chuàng)業(yè)者不再孤獨!

掃一掃,關(guān)注站長網(wǎng)微信

大家都在看

    熱門排行

      最近更新

        返回頂部
        主站蜘蛛池模板: 久久国产精品老人性 | 国产成人久久精品激情91 | 免费亚洲成人 | 嗯~啊~哦~别~别停~啊黑人 | 91成人高清在线播放 | 国产高清一 | 草草影院国产 | 啪视频在线 | 99久久婷婷免费国产综合精品 | 香蕉视频在线观看免费国产婷婷 | 久草综合视频 | 黄网在线观看免费 | 色花五月色婷婷 | 亚洲国产日韩精品 | 视色4se影院在线播放 | 日美毛片| 国产成人www| 亚洲一区二区视频在线观看 | 五月香婷婷| 国产大学生毛片一级高清 | a级特黄的片子 | 一区二区三区四区在线不卡高清 | 色综色| 欧美成人xxxxxxxx在线 | 欧美精品一区二区三区在线播放 | 午夜视频在线观看国产www | 亚洲图色视频 | 国产在线拍小情侣国产拍拍偷 | 欧美成人亚洲欧美成人 | 亚洲 欧美 国产 制服 动漫 | 日本丶国产丶欧美色综合 | 性激烈的欧美暴力三级视频 | 日本一级毛片2021免费 | 成视频年人黄网站免费视频 | 国产成人91一区二区三区 | 国产成人亚洲综合网站不卡 | 中文字幕亚洲日本岛国片 | 放几个免费的毛片出来看 | 成年人网址在线观看 | 在线观看国产小屁孩cao大人 | 在线观看色视频 |