Operating System - HP-UX
1829143 Members
1990 Online
109986 Solutions
New Discussion

Perl CGI: Source html-page

 
SOLVED
Go to solution
David_246
Trusted Contributor

Perl CGI: Source html-page

Hi,

I have a CGI-script that needs to print an html-page containing variables dependent on the choise you make.
Right now I do this using :

#!/opt/per/bin/perl

if ($submit) {
open(FH, "< $htmlfile");
my @contents=;
close(FH);

print "Content type text/html; \n\n";
print "Contents";

This does print my html-page, but does not interpeted the variables($value) as :


Is there a way to source the html-file inside the perl/cgi script, so the $variables are interpeted correct?

Thanks a lot for your help in advance !!!

Regs David


The reason for this is that other people should be able to create html-pages and only need to use specific "names" and a form action should be taken. I will only do the perly thing than.
@yourservice
7 REPLIES 7
Mark Grant
Honored Contributor

Re: Perl CGI: Source html-page

You can use the "eval" approach assuming the variable is already set up in your script.

Something like this

while($line=){
$line =~ s/\$([\w\]\[\$]+)/insertvar($1)/eg;
print $line;
}

And then a function called insertvar that does this

$result=eval(\$$1);
return($result);

I think this is what your're after.

Never preceed any demonstration with anything more predictive than "watch this"
David_246
Trusted Contributor

Re: Perl CGI: Source html-page

Hi,

It does not work. I did what you said :

open(FF, "< $file_v");
$line =~ s/\$([\w\]\[\$]+)/insertvar($1)/eg;
print $line;
close(FF);

And I added another subroutine :

sub insertvar() {
$result=eval(\$$1);
return($result);
}

I don't seem to get it. Please help.

Regs David
@yourservice
Mark Grant
Honored Contributor

Re: Perl CGI: Source html-page

David,

Can we just make sure we are trying to solve the same problem.

I am assuming that within your script are some variables, already defined and that the html file contains the text $some_variable_name here and there which you want to replace with the corresponding variable already set within the perl script and then print.

Never preceed any demonstration with anything more predictive than "watch this"
David_246
Trusted Contributor

Re: Perl CGI: Source html-page

Hi Mark,

That is a 100% true. Like the example of says.

The problem is that nothing is printed anymore. With the @contents at least something got printed :)

Regs David
@yourservice
Mark Grant
Honored Contributor

Re: Perl CGI: Source html-page

Actually, the code doesn't quite work as expected but that because I didn't test it :)

Here is the code taht does what I think you want to do. Note the bracket has moved in the "s/etc etc etc/eg" bit and I simplified the eval.

#!/usr/bin/perl

$myvariable="HELLO THERE";

open FF,"
while($line=){
$line =~ s/(\$[\w\]\[\$]+)/insertvar($1)/eg;
print "$line";
}

sub insertvar(){
return(eval($1));
}
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor
Solution

Re: Perl CGI: Source html-page

For some reason I thought you couldn't have the eval in that search but it seems you can so you can simplify it more but getting rid of the function altogether as in


$line =~ s/(\$[\w\]\[\$]+)/eval($1)/eg;
Never preceed any demonstration with anything more predictive than "watch this"
David_246
Trusted Contributor

Re: Perl CGI: Source html-page

Mark,

You are wonderfull !! (ahum, am already married)
This works terrific :

open(FF, "< $file_v");
while($line=) {
$line =~ s/(\$[\w\]\[\$]+)/eval($1)/eg;
print $line;
}
close(FF);

Thanks a lot for your time in it!!

Regs David
@yourservice