1. perl - practical extraction and report language
2. Created in 1987 by Larry Wall, the UNIX based language has evolved into a powerful tool for the internet. It was designed as a quick-fix patch program for UNIX based systems.
3. its popularity has increased due to its flexibility, portability, usefulness, and its varied features.
4.A PERL script can be created inside of any normal simple-text editor program.
5.a PERL file must be saved with a .pl (.PL) file extension in order to be recognized as a functioning PERL script.
File names can contain numbers, symbols, and letters but must not contain a space. Use an underscore (_) in places of spaces.
firstscript-linux.pl:
firstscript-linux.pl:
#!/usr/bin/perl
print "content-type: text/html \n\n"; ( We have to introduce some HTTP headers so that Perl understands we are working with a web browser,)
-->\n indicates the ``newline'' character; without it, Perl doesn't skip to a new line of text on its own.
print "Hello, Perl!"; (we can print text to the browser with the print function.)
:wq!
Development Tools:Download and install ActivePerl
You first need to download ActivePerl the latest versionfrom http://www.activestate.com/activeperl/. The file we downloaded at the time of this tutorial being written wasActivePerl-5.10.0.1004-MSWin32-x86-287188.msi. You can also find the installation files for other systems and versions via following linkhttp://www.activestate.com/activeperl/downloads/
===>http://www.perlmonks.org/?node=Tutorials
===================
Examples:1
#where perl
#which perl
[sankar@new-host perlscript]$ vim hello.pl
#!/usr/bin/perl
#displays a warm greeting
print "Hello, SHANKAR!\n";
:wq
-----------OR-------------
#!/usr/bin/perl# Displays a warm greeting. my $name = "Sai"; print "Hello, $name!\n";
[sankar@new-host perlscript]$ chmod +x hello.pl
Perl documentation documentation:
===================
Examples:1
#where perl
#which perl
[sankar@new-host perlscript]$ vim hello.pl
#!/usr/bin/perl
#displays a warm greeting
print "Hello, SHANKAR!\n";
:wq
-----------OR-------------
#!/usr/bin/perl# Displays a warm greeting. my $name = "Sai"; print "Hello, $name!\n";
[sankar@new-host perlscript]$ chmod +x hello.pl
OutPut:
[sankar@new-host perlscript]$ perl hello.pl
Hello, SHANKAR!
1. perldocs - Perl documentation documentation
[root@new-host ~]# perldoc perldoc
2. perltoc - perl documentation table of contents
Note:(Any time you want to read a perl manual page,just tell perldoc to display that page)
[root@new-host ~]# perldoc perltoc
3. POD is ``Plain old documentation''. the Plain Old Documentation format
[root@new-host ~]# perldoc perlpod
You can look at the POD source with perldoc's -m switch.
[root@new-host ~]# perldoc -m perlpod
You can convert POD to other formats, including LaTeX, HTML, and text. Look for the pod2latex, pod2html, and pod2text. You need to give it the file name to convert, which you can get with perldoc's -l switch.
5. perlfunc
perlfunc lists the documentation for each perl function, and although you should read it through at least once in your life, you can read the documentation for a single function with perldoc's -f switch.
1. what is the use of #!/usr/bin/perl other than to interpreter?
Floats in Perl can be represented in all the ways that C would allow you to
Here are some examples for you:
- perldoc perldata - to read about Perl data types;
- perldoc -f open - to read about the open() function;
- perldoc -q "how do I do X" - to search the FAQ.
2. perltoc - perl documentation table of contents
Note:(Any time you want to read a perl manual page,just tell perldoc to display that page)
[root@new-host ~]# perldoc perltoc
3. POD is ``Plain old documentation''. the Plain Old Documentation format
You can look at the POD source with perldoc's -m switch.
[root@new-host ~]# perldoc -m perlpod
You can convert POD to other formats, including LaTeX, HTML, and text. Look for the pod2latex, pod2html, and pod2text. You need to give it the file name to convert, which you can get with perldoc's -l switch.
#pod2html `perldoc -l perl` > perl.html
4. perlfaq
There are nine perlfaq pages, broken into broad categories. perlfaq is a table of contents.
You can search the perlfaq pages with perldoc's -q switch.
#perldoc -q perldoc
5. perlfunc
perlfunc lists the documentation for each perl function, and although you should read it through at least once in your life, you can read the documentation for a single function with perldoc's -f switch.
#perldoc -f localtime
6. Modules
Use the perldoc command to read the documentation for installed modules.
#perldoc Module::Starter
7. For instance, you can limit your search to a particular site with in a Google search. This query limits itself to perldoc.perl.org
#perldoc site:perldoc.perl.org CGI
============================
The #! line has two programs that "interpret" it:To the shell.
An executable file that starts with a #! line will cause the executing shell to start up the program indicated in that line.
That way, you can use #!/bin/sh to create a shell script, #!/usr/bin/perl to create a perl script etc. At least on unixish systems, there is always a shell involved in this process.
perl reads the additional arguments on the #! line as a sort of "setup parameters". Example:#!/usr/bin/perl -wT will run perl with global warnings enabled and in taint mode.
The #! line, also called shebang,
=========================================
Perl has three basic data types:
1. scalars,
2. arrays,
3. hashes.
eg:
$a=5;
$b="five"; $c=5.0
perl reads the additional arguments on the #! line as a sort of "setup parameters". Example:#!/usr/bin/perl -wT will run perl with global warnings enabled and in taint mode.
The #! line, also called shebang,
=========================================
The basic data types, three
Perl has three basic data types:
1. scalars,
2. arrays,
3. hashes.
1.Scalars
Scalars are the most basic type of data in Perl they can represent a number(int, float, whatever) or a string of text.eg:
$a=5;
$b="five"; $c=5.0
Here's an example: (Note scalar variables begin with an $)
$a="5.0"; # set up our variables $b="5"; # # to the end of a line is a comment in perl print "Are these variables equal as numbers? ",$a==$b,"\n"; print "Are the variables equal as strings? ",$a eq $b,"\n"; print "These variables are equal as strings\n" if($a eq $b); print "These variables are equal numerically\n" if($a==$b);Integer literals in Perl are pretty straight forward Here are some examples
143 -3 -8431 42 0
Here are some examples for you:
0.1
-123.4
-10e4 # (-10) times 10 to the fourth power
-10E4 # the same thing
-1E5 # Another way to say the same thing
-1e-6 # -1 * 10 to the -6th power
In perl there are two ways to represent string literals:
1.single-quoted strings and
2.double-quoted strings.
Single-Quoted Strings:Single quoted are a sequence of characters that begin and end with a single quote. These quotes are not a part of the string they just mark the beginning and end for the Perl interpreter. If you want a ' inside of your string you need to preclude it with a \ like this \' as you'll see below.
Let's see how this works below.
'four' #has four letters in the string
'can\'t' #has five characters and represents "can't"
'hi\there' #has eight characters and represents"hi\\there" (one \ in the string)
'blah\\blah' #has nine characters and represents "blah\\blah" (one \ in the string)
If you want to put a new line in a single-quoted string it goes something like this'line1
line2' #has eleven characters line1, newline character, and then line2
Single-quoted strings don't interpret \n as a newline. Double-Quoted Strings:
Double quoted strings act more like strings in C or C++ the backslash allows you to represent control characters. Another nice feature Double-Quoted strings offers is variable interpolation this substitutes the value of a variable into the string.
Some examples are below
$word="hello"; #$word becomes hello
$statement="$word world"; #variable interpolation, $statement becomes "hello world"
"Hello World\n"; #"Hello World" followed by a newline
Some of the things you can put in a Double-Quoted String
|
Functions for SCALARs or strings
chomp, chop, chr, crypt, hex, index, lc, lcfirst, length, oct, ord, pack,q/STRING/, qq/STRING/, reverse, rindex, sprintf, substr, tr///, uc, ucfirst, y///
chomp, chop, chr, crypt, hex, index, lc, lcfirst, length, oct, ord, pack,q/STRING/, qq/STRING/, reverse, rindex, sprintf, substr, tr///, uc, ucfirst, y///
2.Arrays
Arrays are basically a collection of scalars stored next to each other and accessed by indices from zero to the number of elements minus one.
Note: when we're referring to an entire array we use the @ at the beginning of its name. If we're referring to only one of its elements(which is a scalar) we use a $.
@a=(1,2,3); @simpsonsfamily=("homer","marge","lisa","maggie","bart");
@a=(1,2,3); @simpsonsfamily=("homer","marge","lisa","maggie","bart");
Arrays can store either strings or numbers or both. Now lets see how we can get at an individual element of an array.
$a[0]; #This returns the first element in @a which is 1
$simpsonsfamily[4]; #This returns the fifth element which is bart
$a[3]=4; #This sets the 4th element in @a to 4.
Note:One nice thing about arrays in Perl is that you don't have to set the size of them when you start so they will grow for you when you need them to.
push is followed by the array you want to add to, and then a value or list of values that you want added to the end. It would look something like:
reverse function simply takes a list or array and reverses it. For example, to reverse the order of an array permanently you would just type something like:push @array, $value; #puts $value onto the end of @array.
Functions for real @ARRAYs@array=reverse @array;
pop, push, shift, splice, unshift
Functions for list data
grep, join, map, qw/STRING/, reverse, sort, unpack
3. Hashes
Hashes are collections of scalars like arrays only they have indices or keys which are strings instead of integers. Hash variables are named with a % followed by at least one letter and then maybe a mix of some more numbers, letters, and underscores. Key and value are two important hash terms.
Now we'll show you how you can initialize and access elements of an array.
@array=("key1","value1","key2","value2"); #an array filled with key+value pairs %hash1=("key1"=>"value1","key2"=>"value2"); #one way to initialize a h+ash (key=>value,key2=>value2,..) %hash2=@array; #making %hash2 out of a co+llection of key value pairs in an array $hash3{"key1"}="value1"; #creates key of "key1" and+ value of "value1" $hash3{"key2"}="value2";============================================Context is everywhere in Perl. Every operand is evaluated in some context. Every expression is constrained by the context in which it is evaluated.The two main contexts are 1. scalar context and 2. list context.Another is void.For these examples, I'll use localtime since it's a common function which evaluates differently according to context. it returns a human readable string to describe the current time.# Example 1. # @now will be (40, 51, 20, 9, 0, 109, 5, 8, 0) @now = localtime(); # Example 2. # $now will be "Fri Jan 9 20:51:40 2009" $now = localtime();========================================================================The Basic Arithmetic Opertators
Operators
Example Type Result $a+$b Addition Sum of $a and $b $a-$b Subtraction Result of $b subtracted from $a $a*$b Multiplication Product of $a and $b $a/$b Division Result of $a divided by $b $a%$b Modulus Remainder when $a is divided by $b $a**$b Exponentiation $a to the power of $b
Perl has its own addition and mulitply operators for strings. These operators are . and x respectively.
$a=2; $a=3; print $a+$b #arithmetic operator prints 5 print $a.$b #string operator prints 2 plus the three or 23 print $a*$b #arithmetic operator prints 6 print $a x $b #string operators prints $a $b times or 2 three times. i +e 222
Assignment Operators
Assignment simply set values on the left side of a = to what is on the right side.
$a=3; $b="x"; $c=4; $a*=3; #$a=$a*3; $a now equal to 9; $a/=3; #$a=$a/3; $a (9) divided by three which equals 3; $a+=2; #$a=$a+2; $a is now equal to 5; $a-=2; #$a=$a-2; $a is now equal to 3; $b x=3; #$b=$b x $3 $b is now equal to "xxx"; $b .="33"; #b=$b."33" $b is now equal to "xxx33";
Comparison Operators:
Type | Numeric | String |
Greater Than | > | gt |
Less Than | < | lt |
Equal to | == | eq |
Not equal | != | ne |
Less than or equal to | <= | le |
Greater than or equal to | >= | ge |
Auto-increment and Autodecrement operators These operators simply add or subtract one from a given variable.
$a=1; print $a++; #prints a as one then adds 1 to it print $a; #now $a is 2 print ++$a; #adds one to $a and then prints its value which is now 3; print $a--; #prints 3 then subtracts one from $a;
Logical Operators:
Examples | Short Version | Textual Version | Meaning |
$a and $b; $a && b | && | and | returns true if $a and $b are both defined and nonzero |
$a or $b; $a||$b | || | or | returns true if either $a or $b is defined and nonzero |
!$a; not $a | ! | not | returns the opposite of what an expression would otherwise |
Precedence for Idiots:
then precedence-awareness of operators probably only goes as far as knowing that 2+3*4 means2+(3*4),
5-1+2 might have you scratching your head - which has precedence - the '-' or the '+'? The answer is 'whichever comes first' - they have equal precedence but left associativity, so 5-1+2 is (5-1)+2, but 5+1-2 is (5+1)-2
For example, ** (the 'to-the-power-of' operator) has right-associativity, so 2**3**4 is 2**(2**3), not (2**2)**3.
Below is a short table that will hopefully make all of this a little clearer.
Function | Meaning | $x is now.. |
$x = 5 == 6 or 5 == 5 | ($x = (5 == 6)) or ($x = (5 == 5)) | FALSE |
$x = (5 == 6 or 5 == 5) | $x = ((5 == 6) or (5 == 5)) | TRUE |
$x = 5 == 6 ¦¦ 5 == 5 | $x = ((5 == 6) ¦¦ (5 == 5)) | TRUE |
($x = 5 == 6) ¦¦ 5 == 5 | ($x = 5 == 6) ¦¦ 5 == 5 | FALSE |
$x = 5 ¦¦ 6 == 6 | $x = (5 ¦¦ (6 == 6)) | 5 |
$x = (5 ¦¦ 6) == 6 | $x = ((5 ¦¦ 6) == 6) | TRUE |
$x = 5 or 6 == 6 | ($x = 5) ¦¦ ($x = (6 == 6)) | 5 |
$x = 1 == 2 && 3 | $x = (1 == 2) && $x = 3 | 3 |
$x = 1 == 2 ¦¦ 3 | $x = (1 == 2) ¦¦ $x = 3 | FALSE |
What is true and false in Perl?
Well for starters "0" and an empty string("") are false. Anything else is basically true.
|
================================================
Flow Control Structures |
1. if statements
if statements test a control statement to see whether it is true or false. else and elsif clauses can also be tagged on the back.
#your basic if if($value==1){ print "value is equal to 1\n"; }#your basic if/else if($value==1){ print "value is equal to 1\n"; } else{ print "value is not equal to 1\n"; }#your basic if/elsif/else if($value==1){ print "value is equal to 1\n"; } elsif($value==2){ print "value is equal to 2\n"; } elsif($value==3){ print "value is equal to 3\n"; } else{ print "value is not equal to 1,2, or 3\n";
}
2. unless statements
Unless statements allow you to say unless something is true do this. For example:
$value=20;
unless($divisor==0){ #so long as the $divisor isn't equal to 0
$value=$value/$divisor; #go ahead and divide $value by $divisor
} else {
$divisor=1; #otherwise set $divisor to 1
}
3. while loops
while loops allow you to execute a group of statements so long as a statment still evaluates to true.
Here's a quick example;
Until loops are just the opposite of whiles. If the control statement evaluates to false code within the loop is executed once and repeated until the control statement tests to true.
Heres a quick example:
$year=1900;
until($year==2000){
print "Mission-critical electronics working in the year $year\n";
#now it works;
$year++;
}
print "Not working anymore\n";
5. do while loops
6. do until loops
|
Uncommon* but Useful Perl Command Line Options for One-liners:
Options covered herein therefor and whencebefore: -e, -p, -n,-l, -0,-a, -F, -M
1. First up: -e (execute)
perl -e '$date=localtime(time); print $date;'
2. Next up: -l (line endings)
perl -e '$date=localtime(time); print $date,"\n";'3. Next up: -n (looping)
perl -l -n -e 'print $1 if /(\w+ear)/' some.txt4. Next up: -p (looping plus)
perl -l -p -e 's/Microsoft/Micro\$\$\$oft/g' some.txt5. Next up: -a (split)
perl -l -a -n -e 'print $F[5]' some.txt6. Next up: -0 (record separator)
7. Finally: -M (& -m) (modules)
-M like use Module;
-m is like use Module();
--------------------------
>>#perl -e 'print "$]\n";'----------->output:5.008008
--------------------------
1. confirm new Perl 5.8.0 is user Perl
>>#perl -e 'print "$]\n";'----------->output:5.008008
>>POD stands for Plain Old Documentation.$ perldoc perlpod
>>=head1 .. =head4 commands (They are called command paragraphs officially.eg:=head1 NAME My::Module - An example module
No comments:
Post a Comment