Problems? Is your data what you think it is? | |
PerlMonks |
Re: CGI, DBI, HTTP, SQL - How does it all fit together?by davido (Cardinal) |
on May 04, 2011 at 18:53 UTC ( [id://902973]=note: print w/replies, xml ) | Need Help?? |
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 Section
Seekers of Perl Wisdom
|
|