• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
Introduction to Perl Programming ( perl 5 )
Contents
 BasicsVariables and Operators Branching  Looping File Test Operators Regular Expressions Input and Output  Processing files mentioned on the Command lineGet Filenames Pipe input and ouput from/to Unix Commands Execute Unix CommandsThe Perl built-in Functions Subroutines Some of the special VariablesForking  Building Pipes for forked Children Building a Socket Connecting to another Computer Get User and Network Information ArithmeticsFormatting Output with "format"Commandline Switches
 
 Basics
Scripts
Perl is a script language, which is compiled each time before running. That unix knowsthat it is a perl script there must be the following header at the topline of every perl script:
#!/usr/bin/perl
 where the path to perl has to be correct and the line must not exeed 32charachters.
Comments and Commands
After the header line:
#!/usr/bin/perl
 there are either empty lines with no effect or command lines or commentary lines. Everything from and behind a "
#
" up to the end of the line is comment and has no effect on the program. Commands start with the first nonspace charachter on a line and end with a ";". So one can continue a command over several lines and terminates it only with the semicolon.
Direct commands and subroutines
 Normal commands are executed in the order written in the script. But subroutines can be placed anywhere and will only be evaluated when called from a normal command line.Perl knows it's a subroutine if it the code is preceded with a "sub" and enclosed in a block like:
sub name { command;}
 
Other special lines
Perl can include other programming code with:
require something
 or with
usesomething
.
Quotations
Single quote:
''
 or:
q//
 Double quote:
""
or:
qq//
 Quote for execution:
``
 or:
qx//
 Quote a list of words:
('term1','term2','term3')
 or:
qw/term1 term2 term3/
 Quote a quoted string:
qq/"$name" is $name/;
 Quote something wich contains "/":
qq!/usr/bin/$file is readdy!;
 
Scalar and list context
 
That perl distinguishes between scalar and list context is the big feature, which makes itunique and more useful then most other script languages.A subroutine can return lists and not only scalars like in C. Or an array gives the number of elements in a scalar context and the elements itself in a list context.The enormous value of that feature should be evident.
Variables and Operators
General
There are scalar variables, one and two dimensional arrays and associative arrays. Insteadof declaring a variable, one precedes it with a special character.
$variable
 is a normalscalar variable.
@variable
is an array and
%variable
 is an associative array. The user of  perl does not have to distinguish between a number and a string in a variable. Perlswitches the type if necessary.
Scalars
Fill in a scalar with:
$price = 300; $name = "JOHN";
 Calculate with it like:
$price *=2; $price = $oldprice * 4; $count++; $worth--;
Print out the value of a scalar with:
print $price,"\n";
 
Arrays
Fill in a value:
$arr[0] = "Fred"; $arr[1] = "John";
 Print out this array:
print join('',@arr),"\n";
 If two dimensional:
$arr[0][0] = 5; $arr[0][1] = 7;
 
Hashes (Associative Arrays)
Fill in a single element with:
$hash{'fred'} = "USA"; $hash{'john'} = "CANADA";
 Fill in the entire hash:
%a = ('r1', 'this is val of r1','r2', 'this is val of r2','r3', 'this is val of r3',);
or with:
%a = (
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...