Re: Pass the value from perl script to html page
by bart (Canon) on May 04, 2005 at 09:28 UTC
|
You generate a HTML page, in a CGI script on the webserver or using mod_perl. In it, you fill in the values you want to appear.
Check ovid's CGI course to learn the basics. mod_perl is quite similar, except harder for a newbie. | [reply] |
Re: Pass the value from perl script to html page
by eibwen (Friar) on May 04, 2005 at 09:27 UTC
|
It's hard to tell what you intend from your post, however HTML has forms with GET and POST methods. CGI is particularly useful in developing such applications/pages (see also Tutorials). If you're just trying to print to the browser from a cgi:
#!/usr/bin/perl
print <<HEADERS;
Content-Type: text/html
HEADERS
print "\n";
print <<PAGE;
PAGE
To output a variable, merely include it within the heredoc and it will be interpolated.
For an example using CGI, consult the synopsis of the linked pod. | [reply] [d/l] [select] |
|
Hi
Thanks both of you for your reply, actualy I have created a saperate html page now let say I want to diaplay tow differnt messages on that html page, say if conditon A is tru in perl script then messages A will be move to a field and pass to html page otherwise message B will be passed to html page. html page is seperate one, not creating in perl script.
| [reply] |
|
| [reply] |
|
<html>
<body>
%MESSAGE%
</body>
</html>
here's the perl script
#!/usr/bin/perl -w
use strict;
open HTML, "test.html" or die "can't open html file!\n";
# read the whole file in a variable
my $htmldata=join '', <HTML>;
close HTML;
# prepare the message to display
my $parameter=shift;
my $message;
if ($parameter >0) { $message="Oh no!"}
elsif ($parameter<0) { $message="Really?"}
else {$message="Mwahahahaha!"}
# replace %MESSAGE% by whatever you want
$htmldata =~ s/\%MESSAGE%/$message/;
# display the modified html
print $htmldata
Try running this script with a parameter like 0, 3, -5 and see what's happening.
of course that's really the quick n' dirty way. The Right Way is to use appropriate modules : CGI to manage the input/output from/to the web server, and HTML::Template to manage the HTML templating.
| [reply] [d/l] [select] |
|
|
|
Re: Pass the value from perl script to html page
by TedPride (Priest) on May 04, 2005 at 13:19 UTC
|
Make a template file with distinctive tags to mark where data should be inserted. Load the file, replace the tags with variables. The following should explain what I mean, minus the loading of the template from a file.
use strict;
use warnings;
my ($text, %var);
$var{VAR1} = 'hawg';
$var{VAR2} = 'mansion';
$var{VAR3} = 'flagpole';
read(DATA, $text, 8192);
$text =~ s/<\?(\w+?)\?>/$var{$1}/g;
print $text;
__DATA__
Once upon a time there was a <?VAR1?> that lived in a
<?VAR2?> on top of a <?VAR3?>.
Note that VAR1, VAR2, VAR3, etc. can be any variable names you want. | [reply] [d/l] |
|
I realize you're just trying to be helpful, but I can't help but think code handouts like this harm a programmer learning perl in the medium term.
What is a new programmer more likely to do:
- Copy and paste this minimally functional templatting system that kinda works, or
- Do the ultimately more productive thing and learn how to install and use modules from CPAN.
Most newer programmers will probably choose the first, lesser productive route (but you know what they say about assumptions).
Just saying. A nudge to use something like HTML::Template with some sample code would probably be more helpful over the long term.
Here's something I whipped up to demonstrate some of what HTML::Template can do.
-Any sufficiently advanced technology is indistinguishable from doubletalk.
My Biz
| [reply] [d/l] |
Re: Pass the value from perl script to html page
by chanio (Priest) on May 04, 2005 at 20:32 UTC
|
You should remember where are the things that you are using:
- 1) Your perl is working at the server in your site.
- 2) But the HTML page, when it is seen by the user, is at HIS OWN browser (no more at your site), ok?
So, are your expecting that all the users of your page are having a working Perl at their computer?
The data that is collected at the HTML page's copy that the user is filling, needs to reach your server's Perl to be processed. Or, if you are programming in JavaScript, the data could be processed at the users browser (if they admit using JavaScript)...
So, to process all the important data filled by any user, you should put some form fields in your page, and a submit button that should send them, through the WWW to your server's cgi script. This is like a .pl file but it ends with .cgi and resides in your /cgi-bin folder. You should read something about CGI programming at this site ( Tutorials).
After having the data, you might show it inside a copy of your original page but built again with your data pasted inside of it. And you just show it, you don't need to save it.
Again, you should read about CGI.
| [reply] |