Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fooling me for ages....

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Ok, this bit of code has been fooling me for ages.

Code:
sub update_home {

$code = $input->param('home');

print $code;

open(CODE2, ">/home/ace-clipart.com/public_html/directory/cgi/admin/templates/$username/home.html.new") || &error("Unable to open home.html template!. Reason: $!");
print $code;
close (CODE2);

open(CODE, ">/home/ace-clipart.com/public_html/directory/cgi/admin/templates/$username/home.html.new") || &error("Unable to open home.html template!. Reason: $!");
@newcode = <CODE>;
close (CODE);

foreach $code (@newcode) {

     $code =~ s/counter1/counter/g;
	      $code =~ s/::grand_total::/grand_total/g;
		       $code =~ s/[category]/category/g;

}

print @newcode;
				


}

Basically, the eventual idea of this code is to replace all occurences of the [counter], [category] and [grand_total] occurences with <%category%>, <%grand_total%> and <%counter%>. However, it has not been this simple.

I have been submitting code that SHOULD have had all of these occurences changed asnd then printted out to the screen correctly. However, it has not been working.

Anyone got any ideas? Cos Im completely out :(

Thanks

Andy
 
Here is a sample of some code that I use for templates. Should be easy enough for you to adapt for your needs....

Code:
open (PAGE, &quot;$preview&quot;) or Die (&quot;Unable to open the file $template. $!&quot;);
     while (<PAGE>)                {
	s/%%field1%%/$field/g;
	s/%%field2%%/$field/g;
	s/%%field3%%/$field3/g;
	print $_;
}
close (PAGE)

Hope this helps!
Jim
 
One thing I noticed is that this line:-

$code =~ s/[category]/category/g;

is probably meant to replace [category] with category but the [] symbols are used in regexs to group chars so try escaping them like so:-

$code =~ s/\[category\]/category/g;

paulf

 
Nope, no luck :(

I tried replacing the regex with something different.

Have a look at Then add somethnig into that HTML, maybe replace the [counter] code right at the bottom of the box. Then submit it. The first code printed is the code before being pulled from the file, and the second is the code where it is pulled from the file and regex'ed. Anyone got any ideas?

I changed the regex to;

foreach $code (@newcode) {

$code =~ s/counter1/counter/g;
$code =~ s/grand_total/grand_total/g;
$code =~ s/[category]/category/g;

}

But still not luck :(

Anyone got any ideas?

Thanks

Andy
 
A few thing I see may be potential problems.

I don't think you have anything in @newcode.

It looks like you take $code and send it to a file with FH CODE2 and then try reading it into @newcode with FH CODE. But the OPEN statement for CODE is for output not input which will make @newcode null. Test that by printing out @newcode after assigning it values.

Current code:

open(CODE, &quot;>/home/ace-clipart.com/public_html/directory/cgi/admin/templates/$username/home.html.new&quot;) || &error(&quot;Unable to open home.html template!. Reason: $!&quot;);
@newcode = <CODE>;
close (CODE);

Try changing it to:

open(CODE, &quot;</home/ace-clipart.com/public_html/directory/cgi/admin/templates/$username/home.html.new&quot;) || &error(&quot;Unable to open home.html template!. Reason: $!&quot;);
@newcode = <CODE>;
close (CODE);


You may also have problems with the line:

print $code;

If you want that to go to CODE2 change that to:

print CODE2 $code;

Otherwise it will be going STDOUT unless you've used
SELECT CODE2;
somewhere prior to the print.


I also agree with the paulfrance's assessment that the brackets '[' and ']' in the RegExp must be escaped or it will find and replace any occurrences of the letters in the word &quot;category&quot;.

Hope this helps.
Andrew
 
Andy,

$code =~ s/[category]/category/g;

is not going to do what you expect - you will need to escape the [] chars, like this:

$code =~ s/\[category\]/category/g;
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top