Cours programming perl

♣ Extrait du coursCours programming perl

Introduction
This book is intended for web designers, entrepreneurs, students, teachers, and anyone who is interested in learning CGI programming. Y ou do not need any programming experience to get started – if you can write HTML, you can also write CGI programs. If you have a web page, and want to write CGIs, then this book is for you.
What is CGI?
“CGI” stands for “Common Gateway Interface.” CGI is the method by which a web server can obtain data from (or send data to) databases, documents, and other programs,and present that data to viewers via the web. More simply, CGI is programming for the web. A CGI can be written in any programming language, but Perl is the most popular,and for the course of this book, Perl is the language we’ll be using.
Why learn CGI?
If you’re going to create web pages, then at some point you’ll want to add a counter, a form to let visitors send you mail or place an order, or something similar. CGI enables you to do that and much more. From mail-forms and counter programs, to the most complex database scripts that generate entire websites on-the-fly, CGI programs deliver a broad spectrum of content on the web today. If you’ve ever looked at a site such as Amazon.com, DejaNews, or Y ahoo, and wondered how they did it… the answer is CGI.CGI experience is also in high demand from employers now; you could substantially improve your salary and job prospects by learning CGI.
Why use this book?
This book will get you up and running in as little as a day, teaching you the basics of CGI scripts, the fundamentals of Perl, and the basics of processing forms and writing simple CGIs. Then we’ll move on to advanced topics, such as reading and writing data to and from files, searching for data in a file, writing advanced multi-part forms like order forms and storefronts, using randomness to spice up your pages, using server-side includes, cookies, and other useful CGI tricks. Things you probably have thought beyond your reach, things you thought you had to pay a programmer to do… all of these are things you can easily write yourself, and this book will show you how .
1-Getting Started
Our programming language of choice for this class is Perl. Perl is a simple language,easy to learn, yet powerful enough to accomplish the most difficult tasks. It is widely available, and is probably already installed on your Unix server. Perl is an interpreted language, meaning you don’t need to compile your script – you simply write your script and run it (or have the web server run it). The script itself is just text code; the Perl interpreter does all the work. The advantage to this is you can copy your script with little or no changes to any machine with a Perl interpreter . The disadvantage is you won’t discover any bugs in your script until you run it.
Basics of a Perl Script
Y ou probably are already familiar with HTML, and so you know that certain things are necessary in the structure of an HTML document, such as the <HEAD> and <BODY> tags, and that other tags like links and images have a certain allowed syntax. Perl is very similar; it has a clearly defined syntax, and if you follow those syntax rules, you can write Perl as easily as you do HTML.
If you’re creating scripts on Unix, you’ll need one statement as the first line of every script, telling the server that this is a Perl script, and where to find the Perl interpreter .In most scripts the statement will look like this:
Debugging a Script
A number of problems can happen with your CGI, and unfortunately the default response of the webserver when it encounters an error (the dreaded “Internal Server Error”) is not very useful for figuring out what happened.
If you see the code for the actual Perl script instead of the desired output page from your CGI, this means one of two things: either you didn’t rename the file with the .cgi extension (perhaps you left it named “first.pl”), or your web server isn’t configured to run CGIs (at least not in your directory). Y ou’ll need to ask your webmaster how to run CGIs on your server. And if you ARE the webmaster, check your server’ s documenta –
tion to see how to enable CGIs in user directories.
2-Perl V ariables
Before you can proceed much further with Perl, you’ll need some understanding of variables. A variable is just a place to store a value, so you can refer to it or manipulate it throughout your program. Perl has three types of variables: scalars, arrays, and hashes.
A scalar variable stores a single (scalar) value. Perl scalar names are prefixed with a dollar sign ($), so for example, $x, $y, $z, $username, and $url are all examples of scalar variable names. Here’s how variables are used:
$foo = 1;
$name = « Fred »;
$pi = 3.141592;
You do not need to declare a variable before using it; just drop it into your code. A scalar can hold data of any type, be it a string, a number, or whatnot. Y ou can also drop scalars into double-quoted strings:
$fnord = 23;
$blee = « The magic number is $fnord. »;

……….

Cours programming perl

Cours complet, PDF Gratuit, Télécharger cours, Télécharger gratuitement, Cours programming perl