开发者

Read and write a text file

How to read and write a text file and put it in the same file? I tried but I got empty files.

My code is :

#!/opt/lampp/bin/perl    
print "Content-Type : text/html\n\n";    
$ref = '/opt/lampp/htdocs/perl/Filehandling/data_1.txt';    
open(REF, "+<", $ref) or die "Uabl开发者_如何学JAVAe to open the file $ref: $!";    
while (<REF>) {    
    if($_=~ m/Others/gi) {
        $_  =~ s/Others/Other/gi;
        print "$_\n";
    }
}

$ref = '/opt/lampp/htdocs/perl/Filehandling/data_1.txt';

open(REF, "+>", $ref) or die "Uable to open the file $ref: $!";
print REF "$_\n";
close REF;
close REF;


perl -pi -e 's/Others/Other/gi' /opt/lampp/htdocs/perl/Filehandling/data_1.txt

The -i option edits a file in-place, i.e. overwrites the file with the new contents.

The s/// operator doesn't substitute anything if there is no match, so you don't have to check with m// if you have a match before attempting a substitution.

The flow control in your script is flawed; you open for reading, then print to standard output (not back to the file), then open for writing, but don't write anything useful (or, well, only the last line you read in the input loop).

What's with the Content-Type: header, do you want to run this as a CGI script? That sounds like a security problem.

Do you only want to print if there is a substitution? If so, try this:

perl -ni -e 's/Others/Other/ig and print' /opt/lampp/htdocs/perl/Filehandling/data_1.txt

Notice the change from -p which adds a read-and-print loop around your script, to -n which only reads, but doesn't print your file.


It seems that all you want is to s/Others/Other/gi in your data_1.txt. You can make it a lot of easier with one line: perl -pi.bak -e's/Others/Other/gi' data_1.txt.

If you still want to read and write into the same file you can check the IO::InSitu module at CPAN.


Fᴍᴛᴇʏᴇᴡᴛᴋ: Learning How to Learn How to Update a Text File in Place

If only you were to Rᴇᴄᴏɴɴᴏɪᴛʀᴇ Tʜᴇ Fᴀʙᴜʟᴏᴜs perlfaq5 Mᴀɴᴘᴀɢᴇ — which is available on every Perl installation worldwide including your own — you would easily learn the answer to your question under that Fᴏʀᴍɪᴅᴀʙʟʏ Aᴘᴏᴛʜᴇᴏsɪᴢᴇᴅ Qᴜᴀɴᴅᴀʀʏ entitled “How do I change, delete, or insert a line in a file, or append to the beginning of a file?”.

Somehow that key first step to problem solving failed to execute.

Learning how to efficiently search the standard Perl documentation set is one of the three keys steps in becoming a proficient Perl programmer. (For the record, the other two are to have a rolodex full of working examples and to have a particular itch you need scratched which you can use Perl to scratch it with.)

If you for whatever reason you either cannot or will not search the standard documentation, and so find yourself coming online begging help in doing something that was long ago answered in that documentation set, you will never be self‐enabling. You may also be perceived as something as a pest.


The Recipe Rolodex

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜