jdlev has asked for the wisdom of the Perl Monks concerning the following question:

I want to add a little feature to my site. We average a certain number of calls answered per day for a customer. I creatd a little perl program that looks at the current seconds since epoch, and then adds the total number of calls we estimate that we've taken. The program works great. I wanted to add it to our website. Is there any danger in adding perl code to a web page? If there really isn't that much of a risk, how do I add one of the variables from the perl code to the webpage? It would need to run the program each time the program is opened so it can calculate the correct information for the web page. Thanks for any help!

I love it when a program comes together - jdhannibal

Replies are listed 'Best First'.
Re: Perl Code in HTML
by lostjimmy (Chaplain) on Jan 29, 2009 at 17:27 UTC

    I think you ought to look at the CGI Programming section of the web programming tutorials.

    You can definitely add this functionality to your website, but it's not likely that you will be adding Perl code into an HTML page. You need to set up your Perl script to output the entire web page. Another option is to use Server Side Includes.

    Update: You can embed Perl in text files using ePerl if you really want to, but I'm not sure how well (if at all) that package works.

      Don't use ePerl. It's a long-dead project. There are many ways to add perl code to HTML, including Mason, Template Toolkit, and many others. I wrote about them here.
        I personally would not use perl for this, but rather php. Don't know if it's taboo to say that on perlmonks, but that's just my opinion.
Re: Perl Code in HTML
by Rodster001 (Pilgrim) on Jan 29, 2009 at 19:17 UTC
    You can get really complex with this (using templates, and OO Perl) which is great but you have a learning curve ahead of you. So, since you are pretty new I suggest this:
    1. Add your script to a cron task
    2. Have your script output its result to a text file in a web accessible directory
    3. Use SSI (server side includes) on your web site to include that text file on the page you want to display it
    4. Option: You can have SSI execute your Perl script which will do the same thing. But, without looking at your script I'd go with the cron (it's a bit safer).
    This is quick and dirty and is fairly easy to implement. But, keep in mind it is quick and dirty. Where something like this gets out of hand is when you have a bunch of these sort of scripts scattered throughout your site. Don't do that :)
Re: Perl Code in HTML
by stonecolddevin (Parson) on Jan 29, 2009 at 19:52 UTC

    You could save yourself a lot of time and use http://www.google.com/analytics

    It's pretty verbose in its statistics and has a lot of cool visitor information features you should check out.

    meh.
Re: Perl Code in HTML
by hangon (Deacon) on Jan 30, 2009 at 08:49 UTC

    One quick & dirty solution is to position an <iframe> in your html file to call your program.

    <iframe src="path_to_your_program" scrolling="no" width="20" height="10" frameborder="0" marginheight="0" marginwidth="0" />

    Then add this to your program to print your variable to the iframe.

    print "content-type: text/html\n\n"; print $variable;