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: Regular Expressions: Re: [kvn] Substitutions in an html file: Edit Log



Zhris
Enthusiast

Jun 26, 2010, 12:55 AM


Views: 4263
Re: [kvn] Substitutions in an html file

Hi,

I think you forgot to include your code. However here is an example (untested):


Code
#!/usr/bin/perl            
use strict;

my $tempfile = 'tempfile.html';
my $htmlfile = 'htmlfile.html';
my $search = "<\?=\$_\['\.TEXT\.'\]\?>";
my $replace = '<?=NLS(.TEXT.)?>';

open (FH1, "<$tempfile") || die "Cannot Open FH1 - $tempfile - $!";
open (FH2, ">$htmlfile") || die "Cannot Open FH2 - $htmlfile - $!";

while (my $line = <FH2>) {
chomp $line;
$line =~ s/$search/$replace/g;
print FH1 "$line\n";
}

close (FH2);
close (FH1);

rename($tempfile, $htmlfile);


Note that special characters which are used for pattern matching must be escaped whilst searching. Or you could use /\Q$search\E/ , however I think the $ will still need to be escaped to ensure $_ isn't interpolated.

Chris


(This post was edited by Zhris on Jun 26, 2010, 12:29 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 12:57 AM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 1:02 AM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 1:07 AM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 1:13 AM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 1:14 AM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 1:14 AM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 1:15 AM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 1:16 AM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 1:16 AM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 1:19 AM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 1:19 AM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 1:20 AM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 1:21 AM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 12:29 PM
Post edited by Zhris (Enthusiast) on Jun 26, 2010, 12:29 PM


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

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