I am looking to find a string in a text file and replace with a new value, here is the code I am having difficulty with, can anyone help find out why I can not print to the text file with the new value found? Thank you!!! Here is the test.txt
<?--1-News1--> <b>This is option News 1 of 1</b> </?--1-News1--> <?--1-Weather2of1--> <b>This is option Weather 2 of 1</b> </?--1-Weather2of1--> <?--1-Stock3of1--> <b>This is option Stock 3 of 1</b> </?--1-Stock3of1--> <?--2-Second1of2--> <b>Option Second 1 of 2</b> </?--2-Second1of2--> <?--3-Third1of3--> <b>Option Third 1 of 3</b> </?--3-Third1of3-->


Here is the program I am trying to write:

use strict; use CGI qw(:header); use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; print "content-type: text/html\n\n"; #&getdata(%in); &getdata; #$location = $in{'location'}; #$obj_name = $in{'obj_name'}; #$page = $in{'page'}; #The value on these variable are hard coded for testing #my $location=param('location'); $location="1"; #my $obj_name=param('obj_name'); $obj_name="News1"; $page="Testing New Content for Object ONE TWO"; my $filename="test.txt"; my $template_data = "unsecure/".$filename; undef $/; # Slurp mode open(DATA_IN, "$template_data") || print "Can't open output file1: $te +mplate_data\n"; #binmode DATA_IN; $_ = <DATA_IN>; #close DATA_IN; while(/<\?--([^-]*)-([^-]*)-->(.*?)<\/\?--([^-]*)-([^-]*)-->/sg){ #<?--1-News1--> #<b>This is option News 1 of 1</b> #</?--1-News1--> $a=$1;$b=$2;$c=$3;$d=$4;$e=$5; if ($a eq $location){ if($b eq $obj_name) { #$c=~s/$c/$page/ig; $c=$page; open( DATA_OUT, ">/unsecure/test.new" ) or d +ie "$!\n"; print DATA_OUT "<?--$a-$b-->\n"; print DATA_OUT "$c\n"; print DATA_OUT "</?--$d-$e-->\n"; + } } } #print DATA_OUT; close DATA_OUT; close DATA_IN; #print "content-type: text/html\n\n"; print "<b> EDITOR</b><br><br>\n"; print "<hr>"; print "You're about to change location:&nbsp;&nbsp;&nbsp;&nbsp;<b>$loc +ation</b>"; print "<br>\n"; print "Viewing object name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>$obj +_name</b>"; print "<hr>\n"; print "<br><br><br><br>\n"; print "<b>$page</b>"; print "<br><br><br><br>";; print "<hr>"; print "<form>\n"; print " <input type=\"submit\" value=\"submit\">\n"; print "</form>\n";

I cant see where the problem is. Thanks again!!!

In reply to Search and Replace Text File by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.