亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2013年02月05日 14:28:55   作者:  
有關(guān)perl文件操作的一些例子,供大家學(xué)習(xí)參考

刪除文件

使用unlinke函數(shù),比如unlink $file, unlink $file1, $file2, $file3

打開文件

使用三參數(shù)的形式打開文件,這樣非常便于區(qū)分模式和文件名,perl 5.6之后的版本都支持這種方式。

復(fù)制代碼 代碼如下:

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

復(fù)制代碼 代碼如下:

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

復(fù)制代碼 代碼如下:

#!/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)插。

復(fù)制代碼 代碼如下:

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

文件句柄作參數(shù)

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

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

復(fù)制代碼 代碼如下:

sub main {
    open FILE, '+<', 'test.data' or die $!;
    &test(*FILE);
    close FILE;
}

方法二,使用open my $FILE的形式打開文件

復(fù)制代碼 代碼如下:

sub main {
    open my $FILE, '+<', 'test.data' or die $!;
    &test($FILE);
    close $FILE;
}

相關(guān)文章

  • Perl 和 StrawberryPerl 與 ActivePerl 的區(qū)別詳解

    Perl 和 StrawberryPerl 與 ActivePerl 的區(qū)別詳解

    這篇文章主要介紹了Perl 和 StrawberryPerl 與 ActivePerl 的區(qū)別詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Perl 字符串處理備忘錄

    Perl 字符串處理備忘錄

    Perl字符串處理學(xué)習(xí)備忘錄,包括chop和chomp函數(shù)、getc函數(shù)、拼接字符串、分割字符串、重復(fù)拼接字符串、字串替換等。有需要的朋友,可以參考下
    2013-02-02
  • Perl 批量添加Copyright版權(quán)信息

    Perl 批量添加Copyright版權(quán)信息

    對所有輸入文件,如果沒有版權(quán)信息則加上版權(quán)信息,否則什么都不做,并對原文件以.bak結(jié)尾備份,需要的朋友可以參考下
    2017-08-08
  • python urllib中的編碼處理示例

    python urllib中的編碼處理示例

    這篇文章主要介紹了python urllib中的編碼處理示例,本文直接給出幾個處理實例,需要的朋友可以參考下
    2015-04-04
  • Perl AnyEvent中的watcher實例

    Perl AnyEvent中的watcher實例

    這篇文章主要介紹了Perl AnyEvent中的watcher實例,關(guān)于AnyEvent請參閱的更多介紹請參閱文中的相關(guān)鏈接,需要的朋友可以參考下
    2014-09-09
  • Perl實現(xiàn)刪除Windows下的圖片緩存縮略圖Thumbs.db

    Perl實現(xiàn)刪除Windows下的圖片緩存縮略圖Thumbs.db

    這篇文章主要介紹了Perl實現(xiàn)刪除Windows下的圖片緩存縮略圖Thumbs.db,本文實現(xiàn)了批量刪除Thumbs.db文件,需要的朋友可以參考下
    2014-12-12
  • Perl批量下載Gmail附件的代碼

    Perl批量下載Gmail附件的代碼

    這篇文章主要介紹了Perl批量下載Gmail附件的代碼,本文重點在使用Mail::POP3Client模塊和Gmail通信的方法,需要的朋友可以參考下
    2014-06-06
  • Perl中的10個操作日期和時間的CPAN模塊介紹

    Perl中的10個操作日期和時間的CPAN模塊介紹

    這篇文章主要介紹了Perl中的10個操作日期和時間的CPAN模塊介紹,本文介紹了Date::Manip、DateTime、Time::Format、Time::Interval、Date::Convert、Benchmark、Time::Normalize、Regexp::Common::time等10個模塊,需要的朋友可以參考下
    2015-02-02
  • 求婚示愛的Perl代碼之改寫篇

    求婚示愛的Perl代碼之改寫篇

    求婚示愛的Perl代碼之改寫篇,特分享下, 方便需要的朋友
    2013-03-03
  • perl控制流介紹(if條件,while,for循環(huán),foreach)

    perl控制流介紹(if條件,while,for循環(huán),foreach)

    Perl控制流(if條件,while,for循環(huán)),需要的朋友可以參考下
    2013-02-02

最新評論