How do I add lines to the top and bottom of a file in Perl?

后端 未结 9 1132
自闭症患者
自闭症患者 2021-01-06 23:38

I want to add a line to top and bottom of the file. I can do it following way.

open (DATA, \"         


        
9条回答
  •  清酒与你
    2021-01-07 00:06

    Three answers have been given that perpetuate the very bad practice of:

    open(FILE,"

    Not only that, the code is broken since you are not opening the file for writing but for reading.

    When an open fails, you can tell the user why it failed. Please get in the habit of including $! in the error message. Also, use the three argument form of open to separate the mode from the name:

    my $path="file";
    open my($fh), '>', $path or die "$path: $!";
    

    (This does not answer your question, but I'm making it an answer rather than a comment for added emphasis and so that I can review it as it is a rather lengthy spewing forth.)

提交回复
热议问题