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

Fellow monks,

I have traversed my books and have failed to come up with a decent answer to this question.

Is it possible to have a cgi script activate on a web page automatically? I know it's possible for a counter, but what about other scripts?

I'm redesigning my web page and want to make it sort of dynamic on the first page - so I thought Perl/CGI right off the bat. I can whip that up in no time at all ( except for maybe the graphics...).

Am I going down the wrong road with this? I can't see how javascript will do a dynamic page. Should I be looking in another direcction?

Thanks in advance!

Some people fall from grace. I prefer a running start...

  • Comment on Automatic execution of a CGI program on a web page besides a counter
  • Download Code

Replies are listed 'Best First'.
Re: Automatic execution of a CGI program on a web page besides a counter
by belg4mit (Prior) on May 26, 2002 at 21:27 UTC
    SSI ("Server Side Includes") (UPDATE: Heh. It was not my intention to link to the first google result, that is actually the tutorial I refer to. The google link was to beef up the comment ;-)

    --
    perl -pew "s/\b;([mnst])/'$1/g"

Re: Automatic execution of a CGI program on a web page besides a counter
by JayBonci (Curate) on May 26, 2002 at 21:48 UTC
    I mean no disrespect if I am incorrect, but perhaps you are confusing server-side with client-side. Perl and CGI(.pm) run server side. The results are dumped to a page, and that is all. You can generate JavaScript code with perl (it's a little tricky, and you'll fall in love with escaping characters all over again), but once it's in the hands of the browser, Perl/CGI stops working.

    The counter example you listed is a little skewed. Counters usually work like:
    <img src="mycounter.pl?acct=mysomething&foo=bar&eep=yah">
    The image is then dynamically generated by the counter script and you have something that has a dynamic feel, when really it's some dynamic image magic being thrown together into a static page, and all of the magic occurs on the server.

    From there, it becomes the responsibility of the web page to be dynamic, and there is the realm of JavaScript.

    Now, the answer to your question relies mostly on what you mean by dynamic. By dynamic do you mean:
    • a) Moving around images, text popups, mouse events, etc. Dynamic as in Dynamic HTML, or...
    • b) Generating dynamic content that requires interaction with a database, cookies, or customization based on users
    The first one maps to using JavaScript, and the second one maps to using Perl and CGI. It's likely you want a combination of both. As a somewhat interesting side note, ActiveState offers PerlScript, which I think can be run as a dynamic browser technology using the DOM, but I've really never tried it. Now THOSE would be some powerful web pages, but that's a strange dependancy. Good luck with your project. If you provide some more details as to what you'd like to accomplish (perhaps demo the direction you'd like to go in), the crazy camel riders might be able to help a little more.

        --jb
Re: Automatic execution of a CGI program on a web page besides a counter
by theorbtwo (Prior) on May 26, 2002 at 21:26 UTC

    If you know how to do it for a counter, then do it just like that, only more so.

    No, really.


    We are using here a powerful strategy of synthesis: wishful thinking. -- The Wizard Book

Update: Automatic execution of a CGI program on a web page besides a counter
by Popcorn Dave (Abbot) on May 27, 2002 at 04:45 UTC
    Thanks to all who offered their help!

    I just got off the phone with my ISP and have found out the following information - your mileage may vary. :)

    I already knew that browsers look for index.html when you type in www.myhost.com

    What I found out was ( and forgive me if this is commmon knowledge ) that failing the index.html page, the next in the sequence is index.htm, then index.*.

    So to make an index page that changes every time the browser opens it, I can do a CGI script to accomplish this and just name it index.cgi and the browser will take care of the rest.

    Like I said above, your mileage may vary.

    p.s. Can someone point me to a node on how to update an original post? Thanks!

    Some people fall from grace. I prefer a running start...

    Update 5/30/02

    After playing around with this last night I discovered that my provider does NOT handle a .pl file well. No matter what your permissions are, it wants to send you the source.

    However changing the extention to CGI worked just fine.

      Actually, that's done by the web server. It's also configurable. If I wanted, I could have the default page in a location be 'monkeys.monkeys.monkeys'. (Half the fun of programming is breaking the expected rules.)

      As for updating top-level nodes, it's disallowed in this section, except for typo fixes. The best approach is to do as you've just done by replying. Good job.

      PopcornDave,

      In regards to what chromatic said, you should look into pointing users that go to "www.myhost.com" to index.shtml. By having an index written with SSI and Perl, i think you'll be able to reap the most benefits.

      I'm not against writing a whole website in Perl, however,

      Actually, this depends greatly on the server-side configuration. For apache, you can define in httpd.conf what files are considered "index" files. On mine, it will check for an index.html, index.cgi, or index.php. If you want to repoint to a CGI type file and index.cgi is not processed, try an index.html with a "refresh" metatag that points to a new page.
      <META HTTP-EQUIV="Refresh" CONTENT="0; URL=/cgi-bin/index.cgi">
Re: Automatic execution of a CGI program on a web page besides a counter
by Anonymous Monk on May 27, 2002 at 12:28 UTC
    I fail to see what's so Perl specific about this question. The answer is 'Yes', but that has absolutely nothing to do with Perl. The answer would be the same, regardless of the language you write your web stuff in.

    Only thing that matters is how HTTP works. The browser issues a request to an HTTP server. The server then responds, usually by sending the object. What happens between the arrival of the request and the sending of the response is up the server. You can do anything you like - in any language you like.

Re: Automatic execution of a CGI program on a web page besides a counter
by BUU (Prior) on May 26, 2002 at 21:09 UTC
    I fail to see exactly what your trying to do. If your just trying to have a script run every so often, look into cron jobs. If your looking for a 'dynamic' homepage, then write the entire thing in perl, and have the script pull and/or execute the other parts you need.
    In short we need more information on what exactly you are trying to accomplish.
      Actually what I was after was using Perl to change the graphic on the front page and the title of the page at random each time someone visited the page. Hence my curiosity to if it could be done in Perl, and how. That's why I queried about the counter example as something that does run without being invoked as a form is for example.

      Some people fall from grace. I prefer a running start...