Click to See Complete Forum and Search --> : Cgi-Perl "hello world" question


web101
03-29-2004, 07:42 PM
I am trying to create forms and make them work and stumbled upon cgi and perl. My compter's OS is windows.

I would like to execute a simple "hello world" kind of program in cgi-perl. Haven't been able to get hold of my Hosting provider to find out what type is their web server. But i saw a cgi-bin directory and figured i cud try to run a simple program. But i am a little too lost here.. I have some basic questions

1> Can someone give me a small code in Perl which when i invoke from the form's submit button that wud display "Hello world" in my browser. Or a pointer to a simple tutorial that wud do so?

2> Can i just key in the Perl program/code in notepad and upload them to my web-server's (provider's server) and will that execute from there?

3>If i place the program under cgi-bin, shud I give a path name from the invoking 'form' as "cgi-bin/.." ?

4> I am also trying to get off my ASP to a start, (all depends on if my ISP supports it, atleast for the imminent present) wud it be a better bet to concentrate and do this stuff with ASP rather than cgi..

Jeff Mott
03-30-2004, 09:58 AM
Have a read through Beginning Perl (http://learn.perl.org/library/beginning_perl/).

silent11
03-31-2004, 08:23 AM
ASP may be easier than perl to pick up as a beginner. now when I say ASP I refer to the technology, you can use perl, JavaScript, VB Script and other languages inside of the ASP technology.

I use perl. I like it much better than ASP. but that's just me. Check out beginning books on both from a library and learn some about each language/technology so you can make a more educated decission for yourself.

I've found perl to be a very creative and expressive language. It provides you with the freedom to do one simple task several different ways. Some like this, while other's dont. You can read more about one instance here on my blog http://silent11.com/blog/archives/000036.html

Good Luck.

dataferret
04-05-2004, 07:26 AM
1> Can someone give me a small code in Perl which when i invoke from the form's submit button that wud display "Hello world" in my browser. Or a pointer to a simple tutorial that wud do so?


Although what you are asking is very simple to achieve you would be best trying to learn what Perl is, does and why before attempting to write your first script. A basic understanding of the coding language and techniques is absolutely necessary before you can start. I found this out the hard way and can promise you, without gaining the basic knowledge, you make things harder for yourself in the longer term. A little homework goes a long way - trust me on this :)

Secondly, there are many ways to write up a Perl script which servers up output <STDOUT>. The easiest to write is where the html code is embedded into the script but this makes it difficult for non-programmers to configure the design of the html page being rendered to the browser. Consider using templates and a template engine for your longer term scripts if you want to makie updating the html easier.


2> Can i just key in the Perl program/code in notepad and upload them to my web-server's (provider's server) and will that execute from there?


Yes - You can write Perl/CGI scripts in notepad and as long as you save them with the extension .pl or .cgi and set the correct permissions on the server they should work fine. A script is nothing more than a text file which contains a series of commands to the perl interpreter. It is the file extension
which tells the server what to do with this text file (i.e. if it is text.pl the server knows it is a perl file).


3>If i place the program under cgi-bin, shud I give a path name from the invoking 'form' as "cgi-bin/.." ?


It is important for you to understand all about file paths. It is equally important for you to understand that not all servers are created equal. Each web server can be set up in a different way and this has an effect on the file paths used in forms etc.
I recommend always using an Absolute path to your forms or scripts rather than a Relative path. An absolute path is a complete url (http://mydomain.co.uk/cgi-bin/myform.pl) whereas a relative path is relative to the cgi-bin (i.e. ../../cgi-bin/myform.pl) which will be different on each server. The advantage of using absolute paths is if you change hosting provider you do not need to rewrite your scripts too much.

Hope this helps

web101
04-07-2004, 06:05 PM
Thank you for all your replies. I have gotten myself perl books and am trying to understand the basics of perl. My current goal is to get the form details emailed to me.

dataferret
04-07-2004, 07:07 PM
Getting hold of Perl books is a great start but be careful not to try anything too advanced. It is far better to work through things slowly - being sure you understand each process before moving onto the next one.

Many people think Perl is easy to learn. True, it is easier than something like C++ and is pretty forgiving when you code loosely, but it is a programming language which talks to other programming languages which in turn talk to a computer and deliver results. For some reason people think they will be able to produce complex Perl applications 24 hours after picking up their book and this is not the case (unless you already know other programming languages).

In your case, you need to think about what your application is going to achieve and deliver. You need to plan it before writing a single line of code. This is not difficult and will save you countless hours in the long term.

So here we go...

As I understand it you want a Perl script with a form in it which when the user clicks a submit button it produces some kind of output printed to the browser (i.e. hello world).

Believe it or not that is the project plan. Now you need to define the script definitions (i.e. what is required to make the above plan happen?)

Keeping things really simple:
(a) you will need a variable which contains the stuff you want to print to the screen.
(b) You will need a method of printing the empty form to the screen and referencing the variable when the button is submitted.
(c) You will need something which returns a message if the user submits an empty form

I suggest you read up on Variables, Scalars and input (STDIN) and output (STDOUT).

I suggest you read up on CGI.PM - This is a module which helps you use html in your scripts and does oodles of super things which make your life easier.

There is much more to writing a well coded script than this but it should hopefully be a good place to start. One solid pice of advice: use lots of comments in your scripts no matter how simple they are. You would be amazed the amount of times I have written a quick and dirty piece of code - only to revisit it months later and wonder what the hell I was thinking because I never left any comments. You will also be amazed at how many times you will re-use chunks of code because you know they work :)

Good luck young SkyWalker :)

web101
04-14-2004, 06:34 PM
Didn't log in here for a while.... Will heed your inputs.
Along these lines, Could you give me a pointer (tutorial? / books?) to understand basics of the hosting Web Servers better (example: different types and functionality etc.).

dataferret
04-14-2004, 07:03 PM
Originally posted by web101
Didn't log in here for a while.... Will heed your inputs.
Along these lines, Could you give me a pointer (tutorial? / books?) to understand basics of the hosting Web Servers better (example: different types and functionality etc.).

I suggest you start with one server type, gain your understanding on this then transfer this knowledge to learn about other server types. This will make the learning process much easier for you.

You need to understand what a web server is, does and why it acts in a particular way. Then you need to understand the differences between the various server platforms out there. Contrary to popular belief, a web server is not a piece of hardware (at least not in the strictest sense for the purposes you wish to learn about). A web server is a piece of software which sits inside a computer (i.e. the hardware), and serves up web page output. There are different types of web server software. IIS (Windows Internet Information Server) is bundled into the Windows operating system and works best for (guess what) Windows based operating systems and Windows NT based operating systems. Apache works great with Unix and Linux. There are several others out there too.

Since the majority of websites are powered by a UNIX based piece of hardware, it follows you would want to learn about the Apache Web server and how to configure it. YOu can get a copy of the Apache web server to work on a Windows based system too, so it is an ideal application to start learning with.

I did a quick search of the Internet and found a pretty good tutorial which should hopefully get you started at http://www.ricocheting.com/server/index.html

Please be aware - it is very important to understand the functions of a web server and the differences between different types.

I hope this helps