PDA

View Full Version : I want to learn cgi.


BarrMan
03-09-2007, 02:37 PM
Does anyone have a good tutorial for learning cgi?
What program should I use to run it?

Thanks.

phoenixshade
03-09-2007, 07:28 PM
I'd assume that by "cgi," you mean server-side programming in a more general sense. Your best options are php or perl. PHP is probably much easier to learn ... at least it was for me. In three days I was able to accomplish what took me weeks to learn in perl (but that might just be because I learned the perl first). Perl is a more robust and powerful language (say the experts) and it runs faster, but the gap has been narrowing.

Many, many servers already have both perl and php installed, so perhaps the easiest way is to create yourself a sandbox folder on your server and just upload your files there for testing. With perl in particular, this can be a bit cumbersome, since compile-time errors mean the program produces no output, so debugging is sometimes a nasty cycle of "upload script – execute script – check error log (most servers have these) – modify script – repeat, if necessary." PHP on the other hand outputs its errors to the document, so you can actually see them without going through the additional steps.

You can also download and install the language locally on your machine (available here: Perl (http://www.perl.com/download.csp), PHP (http://www.php.net/downloads.php)), however you'd need to configure it as a server in order to test results in browsers. (If I'm wrong, I'd appreciate a nudge.)

As for actually writing it, I'd suggest you get a decent editor with syntax highlighting. A quick google search revealed the PSPad freeware editor (http://www.pspad.com/) which appears to support both languages.

I notice from your profile that you have experience in C/C++, so you should be capable of using either language. I've been able to pick up most of my PHP knowlege on the web, but for Perl I absolutely had to get a few books. I personally jump-started with "Perl for Dummies" before moving on to the more challenging— but much more informative — "Programming Perl" (aka "the Camel Book").

Best of luck to you.

FishMonger
03-09-2007, 07:49 PM
Many, many servers already have both perl and php installed, so perhaps the easiest way is to create yourself a sandbox folder on your server and just upload your files there for testing. With perl in particular, this can be a bit cumbersome, since compile-time errors mean the program produces no output, so debugging is sometimes a nasty cycle of "upload script – execute script – check error log (most servers have these) – modify script – repeat, if necessary." PHP on the other hand outputs its errors to the document, so you can actually see them without going through the additional steps
It appears that you have never used use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

phoenixshade
03-09-2007, 08:03 PM
Yeah, I forgot about that one. For some reason, this didn't appear to work when I did a job for a client hosted at bluedomino, so I got into the habit of using the error logs constantly. I guess it just stuck.

BTDSoft
03-09-2007, 09:08 PM
Does anyone have a good tutorial for learning cgi?
What program should I use to run it?

Thanks.

http://www.cgi101.com/book/

should answer most your questions.

What I did as a beginner was download some simple scripts, see what they did then just open them in a text editor and read. Not sure why but most of it seems to explain itself. You will also find a lot of scripts that are very well documented within so as you read thru the script you will find statements describing the functions purpose.

BarrMan
03-09-2007, 10:38 PM
Hey guys, thanks for the help!
phoenixshade: I have a server-side knowledge which is ASP so why should I bother learning PHP?
I want to learn cgi because I heard it could do things I can't do with ASP - What can I do with cgi that I can't in ASP?

Thanks again for the help and links!

FishMonger
03-10-2007, 02:15 AM
Hey guys, thanks for the help!
phoenixshade: I have a server-side knowledge which is ASP so why should I bother learning PHP?
I want to learn cgi because I heard it could do things I can't do with ASP - What can I do with cgi that I can't in ASP?

Thanks again for the help and links!
First, you need to understand that CGI is not a language, it's a set of specifications that govern how external programs intract with web servers. The external programs/scripts can be written in almost any language including ASP.

Part of what you get from learning Perl or PHP is platform independence, so the same scripts will work on Windows or Linux/UNIX systems with little or no modifications. With ASP, you're tied down to M$. IMO, Perl/PHP are also cleaner languages that don't use excessive verboseness.

BarrMan
03-10-2007, 11:15 AM
First, you need to understand that CGI is not a language, it's a set of specifications that govern how external programs intract with web servers. The external programs/scripts can be written in almost any language including ASP.

Part of what you get from learning Perl or PHP is platform independence, so the same scripts will work on Windows or Linux/UNIX systems with little or no modifications. With ASP, you're tied down to M$. IMO, Perl/PHP are also cleaner languages that don't use excessive verboseness.
Thanks, so if what you're saying about PHP is true, why would people bother learning ASP? I saw many big sites that use ASP and I'm pretty sure they could use PHP instead but chosen to use it for some reason.

I didn't quite understand what I can do with CGI that I can't do with a server-side language (even with PHP).

ralph l mayo
03-10-2007, 04:45 PM
First, you need to understand that CGI is not a language, it's a set of specifications that govern how external programs intract with web servers. The external programs/scripts can be written in almost any language including ASP.

ASP isn't a programming language either, it's a script engine that hosts different languages. ASP vs. CGI is for the most part a valid straight-across comparison.

Thanks, so if what you're saying about PHP is true, why would people bother learning ASP? I saw many big sites that use ASP and I'm pretty sure they could use PHP instead but chosen to use it for some reason.

If a Audi is so much better, why does anyone buy a Dodge? Because people have different priorities and in a diverse market will warm to different solutions.

I didn't quite understand what I can do with CGI that I can't do with a server-side language (even with PHP).
This is a fundamental misunderstanding of the concept of CGI. It's a host for server side languages. You can run PHP under CGI. From the ASP side, you can run VB in CGI (but if you do and I end up having to maintain it there will be violence). Understand that the script host (ASP, CGI) is a completely different thing on a completely different abstraction layer than the language (PHP, Perl, VB, and so on).

On the part of the script host you can't do anything with CGI you can't do with ASP, except run your server on *nix. One may be able to host languages the other can't.

On the part of the language, one turing-complete language (ie, every language you would seriously consider doing development work with) is provably equivalent to another in terms of what is possible to program in it. What can be efficiently or elegantly programmed in a given language varies. Sometimes people choose technologies for political or marketing reasons. Sometimes it's legacy code from back when X was all the rage which is cheaper to maintain than rewrite.