溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

perl 文件操作學習

發(fā)布時間:2020-08-09 04:50:20 來源:網(wǎng)絡(luò) 閱讀:1169 作者:nxf198609 欄目:開發(fā)技術(shù)

現(xiàn)有文件hello.txt,內(nèi)容為:"你好'\n' 我是中國人"

1,打開文本hello.txt

      #!/usr/bin/perl

      open f,"hello.txt";

    f 為文件句柄,指向打開的文件


2,逐行讀取文本hello.txt

      #!/usr/bin/perl

      open f,"< hello.txt";

      while($line=<f>){

          print $line;

       }

       close f;

      結(jié)果:你好

                 我是中國人

     或者:print <f>;

     結(jié)果:你好

                我是中國人

     <> 為取行操作符


3,向a.txt中寫入內(nèi)容

   #!/usr/bin/perl

    open f,">a.txt";

    print f "hello,world\nsee you\n";

    close f;

    如果a.txt原本有內(nèi)容,則原內(nèi)容將會被擦除,a.txt的內(nèi)容為

                    hello,world 

                    see you

4,向a.txt中追加內(nèi)容

    #!/usr/bin/perl

      open f,">>a.txt";

      print f "thank you\n";

      close f;

      a.txt的內(nèi)容為 

              hello,world

              see you

              thank you

     

   print 相當于寫命令,別丟了語句結(jié)尾的 “;"


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI