青草久久影院-青草久久伊人-青草久久久-青草久久精品亚洲综合专区-SM双性精跪趴灌憋尿调教H-SM脚奴调教丨踩踏贱奴

17站長(zhǎng)網(wǎng)

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

perl文件操作的一些例子

2022-12-8 15:33| 查看: 2476 |來(lái)源: 互聯(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";

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

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

#!/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)鍵字來(lái)將$/設(shè)置為局部變量,這樣跳出作用域后,$/又恢復(fù)了原來(lái)的值。

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

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

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

open FILE "<$file" or die $! ; #這樣可以。
open FILE '<$file' or die $! ; #這樣就不可以,因?yàn)?file不會(huì)被解釋成變量?jī)?nèi)插。同樣<也不會(huì)被解釋成輸入符號(hào)。

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

本文最后更新于 2022-12-8 15:33,某些文章具有時(shí)效性,若有錯(cuò)誤或已失效,請(qǐng)?jiān)诰W(wǎng)站留言或聯(lián)系站長(zhǎng):17tui@17tui.com
·END·
站長(zhǎng)網(wǎng)微信號(hào):w17tui,關(guān)注站長(zhǎng)、創(chuàng)業(yè)、關(guān)注互聯(lián)網(wǎng)人 - 互聯(lián)網(wǎng)創(chuàng)業(yè)者營(yíng)銷服務(wù)中心

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

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

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

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

大家都在看

    熱門排行

      最近更新

        返回頂部
        主站蜘蛛池模板: vr亚洲成年网址在线观看 | 亚洲永久免费视频 | 亚洲欧美精品一中文字幕 | 性欧美video另类hd高清 | 国内精品久久久久影院亚洲 | 老王午夜69精品影院 | 深夜释放自己在线观看 | 久久超碰色中文字幕 | 赤兔CHINESE最新男18GUY | 狠狠色色综合站 | 美国caopo超碰在线视频 | 伊人亚洲AV久久无码精品 | 久久精品美女 | 伊人久久国产免费观看视频 | 午夜福利理论片高清在线 | 美女教师朝桐光在线播放 | 好大好爽CAO死我了BL | 在线观看免费小视频 | 国内精自品线一区91 | 老师扒开尿口男生摸尿口 | 日韩一区二区三区视频在线观看 | 精品久久久亚洲精品中文字幕 | 伸到同桌奶罩里捏她胸h | 亚洲日韩中文字幕日本有码 | 精品久久久久久电影网 | 92电影网午夜福利 | 在线播放av欧美无码碰 | 91热久久免费精品99 | 日韩美女爱爱 | 女人高潮了拔出来了她什么感觉 | 超碰caopro熟女m超碰分类 | 国产精品人妻系列21P | 一级做a爰片久久毛片潮喷动漫 | 97在线视频免费播放 | 中文字幕人成人乱码亚洲AV | 在线播放性xxx欧美 在线播放午夜理论片 | 亚洲国产在线2020最新 | 久久日本片精品AAAAA国产 | 俄罗斯6一12呦女精品 | 成人国产三级在线播放 | 国产传媒在线播放 |