Gossamer Forum
Home : General : Perl Programming :

Regex, replacing something for a subroutine?

Quote Reply
Regex, replacing something for a subroutine?
Hi. I'm not really sure how praticle this will be... but here goes.

Basically, at the bottom of my Perl script, I have the following;

Code:
__DATA__
sub show_css {
# ------------------------------------------------------------------

print $IN->header();

my $css = q|

<!--
body, td, p {
scrollbar-face-color:#ffffff;
scrollbar-arrow-color:#ffffff;
scrollbar-highlight-color:#ffffff;
scrollbar-3dlight-color:#999999;
scrollbar-shadow-color:#999999;
scrollbar-darkshadow-color:#ffffff;
scrollbar-track-color:#ffffff;
color: #444444;
font-family: verdana, geneva, arial, helvetica, sans-serif;
font-size: 11px;
line-height: 15px;
}
a {
color: #264771;
font-family: verdana, geneva, arial, helvetica, sans-serif;
font-size: 10px;
font-weight: bold;
text-decoration: none
}
a:visited {
color: #264771;
font-family: verdana, geneva, arial, helvetica, sans-serif;
font-size: 10px;
font-weight: bold;
text-decoration: none
}
a:hover {
color: #bf4343;
font-family: verdana, geneva, arial, helvetica, sans-serif;
font-size: 10px;
font-weight: bold;
text-decoration: none
}
a:active {
color: #264771;
font-family: verdana, geneva, arial, helvetica, sans-serif;
font-size: 10px;
font-weight: bold;
text-decoration: none
}
.list {
line-height: 15px;
}
.maintext {
margin-left: 10px;
margin-right: 10px;
color: #333333;
font-family: verdana, geneva, arial, helvetica, sans-serif;
font-size: 11px;
line-height: 20px;
font-weight: normal;
text-decoration: none;
}
.maintext a {
color: #264771;
font-family: verdana, geneva, arial, helvetica, sans-serif;
font-size: 11px;
font-weight: normal;
text-decoration: none;
}
.maintext a:visited {
color: #264771;
font-family: verdana, geneva, arial, helvetica, sans-serif;
font-size: 11px;
font-weight: normal;
text-decoration: none;
}
.maintext a:hover {
color: #bf4343;
font-family: verdana, geneva, arial, helvetica, sans-serif;
font-size: 11px;
font-weight: normal;
text-decoration: none;
}
.maintext a:active {
color: #bf4343;
font-family: verdana, geneva, arial, helvetica, sans-serif;
font-size: 11px;
font-weight: normal;
text-decoration: none;
}

-->

|;


print $css;

}

...basically, I want to open up a file, and add this to the end of it, and also replace;

Code:
if (! $CFG->{setup}) {

...with...

Code:
if ($IN->param('-css-')) { &show_css; exit; }

if (! $CFG->{setup}) {

To do this I am using...

Code:
# redefine the variables :)
my ($read,$write,@read);

# edit admin.cgi, so we point to the CSS stuff...
open(READFILE,$CFG->{admin_root_path}."/admin.cgi") || die "Error reading file " . $CFG->{admin_root_path} . "/admin.cgi Reason: $!";
@read = <READFILE>;
close(READFILE);

$read = join("",@read);

# define the 'replace stuff' and also what we are going to replace it with :)
my $replace = q|if (! $CFG->{setup}) {|;
my $replace_with = q|if ($IN->param('-css-')) { &show_css; exit; }

if (! $CFG->{setup}) {|;

$write = $read;
$write =~ s/$replace/$replace_with/;

$write .= join("", <DATA>); # add on the end stuff we need to put in admin.cgi

# edit admin.cgi, so we point to the CSS stuff...
open(WRITEFILE,">".$CFG->{admin_root_path}."/admin.cgi.new") || die "Error reading file " . $CFG->{admin_root_path} . "/admin.cgi Reason: $!";
print WRITEFILE $write;
close(WRITEFILE);

However,it doesn't seem to be working :(

Anyone got any ideas?

TIA

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Regex, replacing something for a subroutine? In reply to
Nevermind... I figured it out :)

$write =~ s/\Q$replace/$replace_with/;

... I tried it before with \Q in both the sections, and that gave stuff like;

\$var = \"test\.\"\;

... which I didn't need.

The one copy of \Q seems to be working great :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!