What is your background? Are you new to Perl, or just to CGI and Database interaction? Knowing that would help in tailoring a response to the appropriate level of detail.

A CGI program reads the GET or POST data passed to it by the webserver, which it received from the browsing client. The CGI program will then do whatever work it needs to do, and then output a HTTP header followed by the HTML needed to render the resulting web page. The CGI program is, essentially, creating HTML dynamically.

Pretend for a moment that your CGI program ONLY needs to output some predefined HTML. To do so, it just prints it, tags and all. Reading the incoming parameters (GET/POST) is made easier and safer through the use of CGI.pm.

A phrase you'll often hear is that CGI is stateless, which means you don't have a binding between the application and the client. Requests can come in from multiple clients in any order. That sounds complicated, but it's really not too bad. A common way to deal with it is to send a cookie to the client, and read that cookie when the client responds to your web pages. The cookie can be used to maintain a sort of state-ID for a given client. Some people pass a lot of session data to the client too, while others maintain session data server-side, and use the cookie to identify which session belongs to which client.

Your interactions with the database are generally handled through DBI or Class::DBI, though there are many other modules, and many of them are more specific to a web environment. Pretend for a moment that you're NOT creating a CGI script, but instead just using Perl to access a database through DBI. To do so, you open a DBI connection handle, prepare an SQL statement, execute the statement, and then fetch the results.

If you can compartmentalize your thought process it will really help. You take input through CGI's param() function. You send output via print (which should be marked up with HTML). You handle database transactions through DBI.

Later you'll probably want to look at a templating system (such as HTML::Template, a cookie manager such as CGI::Cookie, and session management such as CGI::Session.

I know it's really dated, but you could get yourself a copy of CGI Programming with Perl, 2nd edition (O'Reilly), and a copy of Programming the Perl DBI (O'Reilly). The former is a two to four day read, and the latter is probably two or three days. But having spent a week reading you will save yourself weeks of trial-and-error down the road.

People can feel free to follow-up to my post with newer, more relevant resources. Those are the ones I started with though.

As I browse CPAN I see there's already a blogging module there: App::Plog (Tag-lined "The one and a half minute blog.") That might be helpful to your cause.


Dave


In reply to Re: CGI, DBI, HTTP, SQL - How does it all fit together? by davido
in thread CGI, DBI, HTTP, SQL - How does it all fit together? by tomdbs98

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.