Tuesday, 16 July 2013

how to create a perl program

Creating the Program

A Perl program consists of an ordinary text file containing a series of Perl statements. Statements are written in what looks like an amalgam of C, UNIX shell script, and English. In fact, that's pretty much what it is.
Perl code can be quite free-flowing. The broad syntactic rules governing where a statement starts and ends are:
  • Leading spaces on a line are ignored. You can start a Perl statement anywhere you want: at the beginning of the line, indented for clarity (recommended) or even right-justified (definitely frowned on because the code would be difficult to understand) if you like.
  • Statements are terminated with a semicolon.
  • Spaces, tabs, and blank lines outside of strings are irrelevant-one space is as good as a hundred. That means you can split statements over several lines for clarity. A string is basically a series of characters enclosed in quotes. Chapter 2 for a better definition of, and introduction to strings.
  • Anything after a hash sign (#) is ignored except in strings. Use this fact to pepper your code with useful comments.
Here's Our first uninspired Perl statement hello1.pl:
print("Hello World\n");
No prizes for guessing what happens when Perl runs this code-it prints out Hello World. If the "\n" doesn't look familiar, don't worry-it simply means that Perl should print a newline character after the text, or in other words, go to the start of the next line, exactly like C.
Printing more text is a matter of either stringing together statements like this, or giving multiple arguments to the print() function :
 print("Hello World,\n"); 
 print("I'm  alive\n");
So here's a small finished example hello2.pl, complete with the invocation line at the top and a few comments:
#!/usr/local/bin/perl -w 

 print("Hello World,\n"); 
 print("I'm  alive\n");
Unix has two ways of invoking a Perl program (see below) if you use (or intend to use) Perl on Unix always make this the
You can create your Perl program by starting any text processor:
  • In UNIX -- you can use emacs or vi.
  • In Windows 95/Windows NT -- you can use notepad or edit.
  • In Macintosh -- you can use MacPerl's simple text editor or any other texteditor you like.
  • In OS/2-you can use e or epm.
Create a file called test.pl that contains the preceding three lines. In general convention all Perl files should end with a .pl extension. You can call your program anything you like, but it should be a meaningful description.

0 comments:

Post a Comment