Está en la página 1de 4

PERL

Introduction PERL was developed by Larry Wall (author of the USENET newsreader rn). Wall developed PERL originally as a data reduction language (that is, a language that is capable of consuming large quantities of data in an efficient manner and performing some function(s) on it). PERL is an acronym for "Practical Extraction and Report Language." PERL is almost the perfect tool for system administrators since it allows the easy manipulation of files, process information, and many other items. PERL is also great for people who write cgi-bin scripts to process web forms because it has so many useful string manipulation functions. Since PERL's syntax resembles shell programs and C and it has the builtin functions of awk, sed, and grep, many people find it very easy to learn. One thing to always remember about PERL...."There's always more than one way to do it." Five PERL programmers may write a simple piece of code to perform and function and they may each take five different approaches to accomplish the task. Invoking the PERL Before we get knee deep in PERL, let me first mention how to execute the upcoming examples. Create these files with your favorite text editor then type: % perl myprogram.pl Where myprogram.pl is the name of the file you entered the PERL commands into. (If you get an error saying that perl cannot be found then the location of perl is not in your path OR perl is not installed on your system - check with your system administrator.) The filename.pl is a handy convention for recognizing your perl scripts. #!/usr/bin/perl Be sure that you replace /usr/bin with the appropriate path to your perl binary. Also, be sure that you can execute the script. If you type: % myprogram.pl and get an error similar to: csh: myprogram.pl: cannot execute Then you should probably type: % chmod +x myprogram.pl which will make the program executable. PERL syntax PERL is a free-form language. PERL doesn't care how you write code for the most part. That is, use as much whitespace as you want or as little as you want (with the exception of the format declaration and quoted strings) A PERL script is comprised of declarations and statements.

Your actual PERL code will normally be doing one of two things. Either you are declaring a subroutine (or a report) or you are giving PERL a statement of code. You don't have to declare variables that you will be using ahead of time since any uninitialized variables and arrays start with a null or 0 value until they receive a value of their own. Like C, PERL statements must end in a semicolon (;). Semicolons seperate statements so you can put many statements per line (free-form) but you might sarifice clarity * Comments begin with a pound sign (#). PERL comments work like shell script comments. Anything after an unquoted # in a script is ignored. Comments can begin anywhere on a line. Perl basics This section is about simple things in perl. Comments Perl has only one type of comments - one line comments. Everything what starts with # and up to the end of the line is considered as comment. print "Hello!"; # print out one word Variables To use variables in Perl all you need to do is to initialize them. The names of scalar variables are to start with $ sign: $counter = 0; $amount = 15.20; $name = "Daniel"; print "amount = $amount\n"; As you can see from the example above we don't describe variables we just use them. You can also notice that we do not need to specify a type of a variable. Perl automatically chooses the right type based on the value assigned to a variable. Thus, one variable can store different types of data: $a = 100; print "\$a = $a is a number\n"; $a = 'is a text now'; print "\$a $a\n"; Scalar variables in Perl may hold the following types of values: Integers (decimal, octal, or hexadecimal) Floating point numbers Strings Please notice that Perl does not give any error message if we are trying to use an unitialized variable. For example, the following code print "\$a = $a\n"; will print $a =

If you want to get such messages we would suggest to use option -w for the perl interpreter and package strict in the scripts. The following code # file: warning.pl use strict; print "\$a = $a\n"; if executed by ...> perl -w warning.pl will produce the output: Name "main::a" used only once: possible typo at comments.pl line 3. Use of uninitialized value in concatenation (.) or string at comments.pl line 3. $a = To correct this error we need to declare the variable $a by using the my function: # file: nowarning.pl use strict; my $a = 12; print "\$a = $a\n"; Strings String literals can be created with either single quotes or double quotes. To illustrate difference between them let's consider the following code: use strict; my $name = "Bob"; print "Hello, $name\n"; print 'Hello, $name\n'; This code generates the output: Hello, Bob Hello, $name\n As you can see double quotes strings substitute variable names with their values and also treat backslashes as an escape character, which allows to print special characters like new line character (\n) or a dollar sign (\$). Single quoted strings don't do that. Array variables Arrays in Perl are ordered collection of scalars. Perl allows these scalars to have different types. Array names begin with the @ character: # an array of data my @collection = ("My name", 123, 'string', 3.97);

Perl also allows us to print all elements of the array at once. The following example shows how to do that, please also pay attention to the difference between double and single quotes and also array printed outside of any quotes: use strict; my @collection = ("My name", 123, 'string', 3.97); print "Within double quotes: @collection\n"; print "Outside any quotes: ", @collection, "\n"; print 'Within single quotes: @collection', "\n"; Within double quotes: My name 123 string 3.97 Outside any quotes: My name123string3.97 Within single quotes: @collection As in C++, Perl arrays start at element 0. For @collection, the elements are: 0: "My name" 1: 123 2: 'string' 3: 3.97

También podría gustarte