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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using perl imbedded in a .html page...currently using .pl to generate

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
i currently use this to generate HTML page via .pl:

index.pl

Code:
#!/usr/bin/perl
use CGI qw(:standard);
use DBI;
use CGI::Session;
$cgi = new CGI;
$sid = $cgi->cookie("CGISESSID") || undef;
$session = new CGI::Session(undef, $sid, {Directory=>'C:\Temp'});
$cookie = $cgi->cookie(CGISESSID => $session->id);
$sSid = $session->id();
$a = param("a");
$b = param("b");
$c = param("c");
if (!$a) {$a='home';}
print $cgi->header(-cookie=>$cookie);
print << "EOF";
<html>
<head>
<title></title>
</head>
<body marginwidth=0 marginheight=0 leftmargin=0 topmargin=0>
</body>
</html>
EOF

which works great...but how do i execute perl commands/scripts in an .html file?

something like:

index.html

Code:
<html>
<head>
<title></title>
</head>
<body marginwidth=0 marginheight=0 leftmargin=0 topmargin=0>
EOF

if ($x eq 'test') {print "test";}

print << "EOF";
</body>
</html>


but of course, this won't work. the server can't interpret the perl because it's not a .pl, it's a .html type file...

any ideas?

- g
 
That is inside-out... it is just not how Perl works. Your example is akin to PHP ASP. You might want to look at Perlscript.


Kind Regards
Duncan
 
I think what you are trying to do is maybe like this?

Code:
print $cgi->header(-cookie=>$cookie);
print qq(
<html>
<head>
<title></title>
</head>
<body marginwidth=0 marginheight=0 leftmargin=0 topmargin=0>
);
if ($x eq 'test') {print "test";}
print qq(
</body>
</html>);
That way you can have dynamic content in your webpage.
 
my goal is to have a page called, let's say "index.html", but be able to call perl functions within the page...

maybe i'll check out perlscript...

- g
 
I do this all the time with my sites but I run them with mod_perl on apache. To have perl executed inside a file that oes not have a script mime type without using something like moe perl would be tricky since the handler would have to know it need to interpret perl code.

-Alexander
 
on your site, the hobbypit.com, are you just referencing directories, and the default page for that directory is being shown? such as in:


i don't quite understand:

I do this all the time with my sites but I run them with mod_perl on apache. To have perl executed inside a file that oes not have a script mime type without using something like moe perl would be tricky since the handler would have to know it need to interpret perl code.

can you re-explain? i think you have a way i might be able to use...

- g
 
You should be able to name your perl script index.html, perl doesn't care what the file extension is, it's the shebang line that is important. But if want to run a perl script from the web root you need to setup the server that way.
 

i tried renaming my perl script, from index.pl to index.html, but it doesn't interpret the same, it just shows the actual code in the browser window.

am i misunderstading you?

- g
 
you must add a handler for the htm and html extensions.
But this means that it will send every html file to the perl intepreter, so if you want a normal html-file you must do this:

Code:
#!c:/perl/bin/perl.exe

print "Content-Type: text/html\n\n";
print << 'HTML'

Html content goes here

HTML
print "";

But if you want it so, change the following line in conf/httpd.conf:

AddHandler cgi-script .cgi .pl

TO

AddHandler cgi-script .cgi .pl .htm .html
 
Ok a quick (if I can keep it quick) run down of how my site and others that use mod_perl work.


So in my httpd.conf for apache I have a line that sets the perl handler. It looks like this.

Code:
<Location /collecting/antiques>
  SetHandler perl-script
  PerlHandler Subsec::Topic
</Location>

So on my web server the directory /collecting/antiques directory does not even exist. The perl mod the I wrote Subsec/Topic.pm gets the request and handles it. I hope that makes things a little more clear. If not let me know and I will spend a bit more time writing something up with more examples.

I did not see what web server you are trying to run this one but if you have index.html set up to be executed the directory (if you are using apache) needs to be set as a script alias. On IIS you can only do this by setting the .html extension to be executed by perl. Hope some of this hear makes sense and is helpful as I am kinda tired and out of it =)

-Alexander
 
ok, i got it...

this is what i need:

AddHandler cgi-script .cgi .pl .htm .html

it will allow me to have my own extension, in theory...right?

like:

AddHandler cgi-script .cgi .pl .htm .html .myOwn

curiously, how can i show the same file, let's say index.pl, no matter what is in the address bar after the webaddress...

such as:


all will show the same file, index.pl, but the dynamic contents the page will display is based on what follows the webaddress...

ex.

/cheese
/donuts
/sugars/brown-sugar.html

similar to alexv2's example, where instead of:


he has:


i want:


to be interpreted the same way...

does that make sense?

- g
 
I think this post is not answering the original question ...

spewn wants this to work:-

Code:
<html>
<head>
<title></title>
</head>
<body marginwidth=0 marginheight=0 leftmargin=0 topmargin=0>
EOF

if ($x eq 'test') {print "test";}

print << "EOF";
</body>
</html>

If you send that to the Perl interpreter it will choke! Which is why i recommended Perlscript

Please excuse me if i have missed something!


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top