CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Not able to read the file into string

 



cool_cake20
Novice

Jul 14, 2014, 10:44 PM

Post #1 of 3 (892 views)
Not able to read the file into string Can't Post

 
Hi ,

I am trying to read a file and appending it to another file.
Modify the another file and return the content as string.






Code
sub replace_trivial{ 
my($t,$fname) = @_;
open (my $DATA1, "<$fname");
open (my $DATA2, ">>file2.txt");
while(my $line = <$DATA1>)
{
if($line =~ /^ovm_report/)
{
$line=~ s/ovm_report_(info|warning|error|fatal)/sprintf("`ovm_%s",lc($1))/ge;
}
print $DATA2 $line;
}

$t .= <DATA2>;
unlink ("file2.txt");

print "COOL_CAKE \n $t";
}


This code is returning the first file content but not the second file


FishMonger
Veteran / Moderator

Jul 15, 2014, 6:25 AM

Post #2 of 3 (885 views)
Re: [cool_cake20] Not able to read the file into string [In reply to] Can't Post

"file2.txt" was opened in append mode so trying to read from that filehandle when assigning $t isn't going to work and if you had warnings and strict pragmas loaded, perl would have told you about that problem.

Once you appended the lines to second file, you deleted that file without doing anything with its contents.


(This post was edited by FishMonger on Jul 15, 2014, 6:27 AM)


Laurent_R
Veteran / Moderator

Jul 15, 2014, 9:57 AM

Post #3 of 3 (853 views)
Re: [cool_cake20] Not able to read the file into string [In reply to] Can't Post

It seems that you not very clear yourself as to which file you want to append to which other file.

Your while loop is effectively appending the (modified) content of file 1 to file 2. If this what you need, just remove any code currently after the end of the loop.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives