Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

CGI layout

by marlin (Initiate)
on Oct 13, 2011 at 16:10 UTC ( [id://931297]=perlquestion: print w/replies, xml ) Need Help??

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

Hello!

I am very new to Perl and CGI scripting. I know a fair bit of the Perl language but I'm having trouble putting together a CGI-driven website.

How are CGI-run websites usually organized? Is there a single script that is called when a user visits the website, and then that single script will perform all the website functions like accepting inputs, writing to a database and generating html for all the pages?

Or, is there a directory of perl scripts that are embedded into the html pages of the website? And they can each pass info to each other?

Currently I have it set up so that index.html has a Perl script embedded in the center and it outputs all the main content, but how do I make it clear the page or switch to another script, and is that even a good idea?

My goal is to make a server-side Perl program that will accept inputs from a web page, do several computations with these inputs, and then post the results in HTML (and eventually SVG) and continuously do this on the same page without having to refresh.

Replies are listed 'Best First'.
Re: CGI layout
by davido (Cardinal) on Oct 13, 2011 at 17:07 UTC

    CGI still exists, and hasn't died yet. That's about all that it has going for it though. Nowadays MVC frameworks are all the rage. Mojolicious is great. But everyone has their favorites, including Dancer, Catalyst, and even grand-daddy, CGI::Application.

    With a framework like Mojolicious the process is pretty simple. Take a look at the documentation on the link I provided and see if that seems easier.

    Two disadvantages that plain old CGI have are that every time a new hit comes in the Perl interpreter has to fire up all over again, which takes time. And second, non-trivial applications have a habit of becoming too complicated from a coding standpoint since plain old CGI.pm doesn't do anything to help promote separation of concerns (Model, View, Controller).


    Dave

Re: CGI layout
by Khen1950fx (Canon) on Oct 13, 2011 at 16:34 UTC
    You might find Dancer easier to use. It's light, quick, and scalable with support for Plack and PSGI.
Re: CGI layout
by aaron_baugher (Curate) on Oct 13, 2011 at 20:32 UTC

    As others said, if most of your pages will have dynamic content, look into frameworks. To answer your first questions, it varies. Some people have a single CGI that runs everything, calling functions in other files depending on the pathinfo (the way PHP apps like Wordpress or Drupal run everything through index.php). Others use multiple CGIs, each for a different function. Others use SSI, which allows you to embed Perl scripts (or other executables) in HTML, but in a pretty inflexible way, so that's not common. But look at frameworks.

    As for your last requirement: if you want a portion of a page updated regularly without refreshing the whole page, you'll want to look at client-side solutions like Ajax. You can push updates from the server side with Apache's nph-script method, but I've always thought that's pretty ugly. You can also send HTTP headers or meta tags that will cause automatic refreshes, but then you're refreshing the whole page, which you said you don't want. (Those might work within an iframe to do part of a page; I'm not sure about that.) But sites that do dynamic updates to individual divs on the fly use Ajax or something similar, to fetch new content and change just what elements need to be changed. I think all the common frameworks come with a fair amount of support for Ajax built in, too.

Re: CGI layout
by marlin (Initiate) on Oct 13, 2011 at 19:30 UTC
    Oh okay, so frameworks are the way to go. I'll have to look into them. Thank you Khen1950fx and davido.
Re: CGI layout
by Anonymous Monk on Oct 14, 2011 at 00:41 UTC
    "Do not do a thing already done." As "interesting an exercise" as it may or may not be, when you know that you are following in a footpath that has been trod by literally tens of thousands of people ahead of you, you do not want to follow in their footsteps: you want to climb upon their shoulders.
Re: CGI layout
by nando (Acolyte) on Oct 14, 2011 at 06:34 UTC

    I am in a similar situation. From what I see, Mojolicious and Dancer could give my little project that lacks strength.

    I suspect that if I can understand the general concept of these applications the learning curve is not too hard.

    To organize a little all files that have been generated, had thought of studying CGI:: Application ... Would it be better to learn directly Mojolicious or Dancer?

    Thank you!

        cgiapp-starter --module=MyCGIAppApp --author=author --email=email
        
        titanium-starter --module=MyTitaniumApp --author=author --email=email
        
        cas-starter --module=MyCASApp --author=author --email=email
        
        will work if you install cpan PETDANCE/Module-Starter-1.54.tar.gz

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://931297]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-18 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found