温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

perl 文件操作学习

发布时间:2020-08-09 04:50:20 来源:网络 阅读:1160 作者:nxf198609 栏目:开发技术

现有文件hello.txt,内容为:"你好'\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;

      结果:你好

                 我是中国人

     或者:print <f>;

     结果:你好

                我是中国人

     <> 为取行操作符


3,向a.txt中写入内容

   #!/usr/bin/perl

    open f,">a.txt";

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

    close f;

    如果a.txt原本有内容,则原内容将会被擦除,a.txt的内容为

                    hello,world 

                    see you

4,向a.txt中追加内容

    #!/usr/bin/perl

      open f,">>a.txt";

      print f "thank you\n";

      close f;

      a.txt的内容为 

              hello,world

              see you

              thank you

     

   print 相当于写命令,别丢了语句结尾的 “;"


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI