user477189
user477189

Reputation: 21

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;
}

Upvotes: 1

Views: 16278

Answers (4)

Karl Swartz
Karl Swartz

Reputation: 1

If all you want is the time, just use SSI:

<!--#config timefmt="%d %b %Y %H:%M" -->
<p>The current time is <!--#echo var="DATE_LOCAL" --></p>

You will need to tell he server to parse it, in your .htaccess:

AddType text/html .shtml
AddHandler  server-parsed .shtml

Or, as brian suggests, just use Javascript.

Upvotes: 0

brian d foy
brian d foy

Reputation: 132802

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.

Upvotes: 3

DVK
DVK

Reputation: 129403

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>

Upvotes: 1

Chetan
Chetan

Reputation: 48011

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.

Upvotes: 3

Related Questions