开发者

How can I call a Perl script inside HTML page?

I have a single HTML file, how I use a Perl script(date/hour) in the HTML code? My goal: show a date/hour in HTML Obs.: alone both script are ok.

Example: HTML File:

<html>
<body>
code or foo.pl script
</body>
</html>

Perl script(foo.pl):

#!/usr/local/bin/perl
use CGI qw/:push -nph/;
$开发者_开发知识库| = 1;
print multipart_init(-boundary=>'----here we go!');
for (0 .. 4) {
    print multipart_start(-type=>'text/plain'),
          "The current time is ",scalar(localtime),"\n";
    if ($_ < 4) {
            print multipart_end;
    } else {
            print multipart_final;
    }
    sleep 1;
}


Perl is a server-side language, so it must be run on the server. The HTML code is displayed in the browser, and it is generated by the server. So you would have to run the perl script on the server to generate the date / hour, and embed that into the HTML code that you serve to the browser.

Here is a tutorial on how to do this.


It sounds like you want Ajax. Your HTML page uses JavaScript to call your Perl program. Your JavaScript gets the response and replaces the part of the page where you want the data to go. Alternatively, you can just skip the Perl bit altogether and just do it all in JavaScript.


You can either generate the entire HTML page via a CGI script (as per Chetan's answer) - or as an alternative you can use one of the templating modules (EmbPerl, Mason, HTML::Template, or many others).

The templating solution is better for real software development, where separation of HTML and the Perl logic is more important. E.g. for EmbPerl, your code would look like:

<html>
<body>
[- my $date_hour= my_sub_printing_date_and_hour(); # Logic to generate -]
[+ $date_hour # print into HTML - could be combined with last statement +]
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜