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

17站長網

17站長網 首頁 腳本 perl 查看內容

perl文件操作的一些例子

2022-12-8 15:33| 查看: 2456 |來源: 互聯網

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

刪除文件

使用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() ;
1;

最好的方法是使用模塊,這樣比自己寫安全,File::Slurp、IO::All都可以的。

打開文件請用雙引號

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

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

文件句柄作參數

假設有一個函數test,它有一個參數,是某個文件句柄,那么該如何傳遞這個參數呢?

方法一,傳遞參數時,在句柄前面加*


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,某些文章具有時效性,若有錯誤或已失效,請在網站留言或聯系站長:17tui@17tui.com
·END·
站長網微信號:w17tui,關注站長、創業、關注互聯網人 - 互聯網創業者營銷服務中心

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

17站長網微信二維碼

始終以前瞻性的眼光聚焦站長、創業、互聯網等領域,為您提供最新最全的互聯網資訊,幫助站長轉型升級,為互聯網創業者提供更加優質的創業信息和品牌營銷服務,與站長一起進步!讓互聯網創業者不再孤獨!

掃一掃,關注站長網微信

大家都在看

    熱門排行

      最近更新

        返回頂部
        主站蜘蛛池模板: 亚洲大片免费看 | 久久精品中文字幕 | 国产精品高清m3u8在线播放 | 一区二区中文字幕在线观看 | 亚洲国产成人爱AV在线播放丿 | 拔萝卜电视剧高清免费 | 青青草国产偷拍在线av | 小小水蜜桃3视频在线观看 小向美奈子厨房magnet | 在线视频免费国产成人 | 在教室伦流澡到高潮H免费视频 | 最新国产av.在线视频 | 被六个男人躁到一夜同性 | 亚洲一区综合图区 | 青青草A在在观免费线观看 青青草AV国产精品 青青草 久久久 | 香蕉在线播放 | 欧美午夜精品久久久久久浪潮 | 国产一区私人高清影院 | 日本一区二区三区在线观看网站 | 国内精品久久久久影院亚洲 | 国色精品VA在线观看免费视频 | 日日踫夜夜爽无码久久 | 色多多污污版免费下载安装 | 亚洲精品久久久无码 | 99久久婷婷国产综合精品青草 | 久久久久久久电影 | 战狼4在线观看完免费完整版 | 闺蜜撬开我的腿用黄瓜折磨我 | 日本高清免费一本在线观看 | 年轻的朋友4在线看中文字幕 | 亚洲VA欧美VA天堂V国产综合 | 狠狠色香婷婷久久亚洲精品 | 亚洲黄色官网 | 暖暖日本免费播放 | 男人和女人一级黄色大片 | 被老头下药玩好爽 | 97夜夜澡人人爽人人模人人喊 | 女人一级毛片免费视频观看 | 日日久久狠狠8888偷偷色 | 亚洲精品无码午夜福利在线观看 | 丰满少妇被猛烈进出69影院 | videos gratis欧美另类 |