karate_kid
karate_kid

Reputation: 155

Concatenation of new line- Perl

I want to replace a string "hello" with "#hello" . "\nHi".. Here is what I have done..

#!/perl/bin/perl
use strict;
use Tie::File;

my $filename = "abc.txt";
tie my @lines, 'Tie::File', $filename or die;

for(my $i=0; ; $i++) {
    last if !defined $lines[$i]; 

    if($lines[$i]= /^[\s]*hello*/){
        $lines[$i] = "#" . $lines[$i] . "\nHi";
        last;
    }

But new line thing isn't working. Does anyone know the answer?

Upvotes: 0

Views: 687

Answers (1)

Amadan
Amadan

Reputation: 198334

From Tie::File docs:

Inserting records that contain the record separator string is not supported by this module. It will probably produce a reasonable result, but what this result will be may change in a future version. Use 'splice' to insert records or to replace one record with several.

Upvotes: 3

Related Questions