Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Lecture 6 - Perl Introduction

Perl is a high-level scripting language introduced in 1987 designed for text manipulation, system administration, and programming. It can be used for quick scripts, parsing data files, CGI scripts, and complex programming through its many libraries. While slower than C, Perl is faster than shell scripting languages and combines the functionality of many Unix tools. It is an interpreted language that compiles at runtime and is available on many platforms.

Uploaded by

ahmed arab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 6 - Perl Introduction

Perl is a high-level scripting language introduced in 1987 designed for text manipulation, system administration, and programming. It can be used for quick scripts, parsing data files, CGI scripts, and complex programming through its many libraries. While slower than C, Perl is faster than shell scripting languages and combines the functionality of many Unix tools. It is an interpreted language that compiles at runtime and is available on many platforms.

Uploaded by

ahmed arab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Lecture 6:

Perl Introduction
Perl
"Practical Extraction and Reporting Language"
written by Larry Wall and first released in 1987
Perl has become a very large system of
modules
name came first, then the acronym
designed to be a "glue" language to fill the gap
between compiled programs (output of "gcc",
etc.) and scripting languages
"Perl is a language for easily manipulating text,
files and processes": originally aimed at
systems administrators and developers
What is Perl?

Perl is a High-level Scripting language


Faster than sh or csh, slower than C
No need for sed, awk, head, wc, tr, …
Compiles at run-time
Available for Unix, PC, Mac
Best Regular Expressions on Earth
What’s Perl Good For?

Quick scripts, complex scripts


Parsing & restructuring data files
CGI-BIN scripts
High-level programming
 Networking libraries
 Graphics libraries
 Database interface libraries
What’s Perl Bad For?

Compute-intensive applications (use C)


Hardware interfacing (device drivers…)
Executing Perl scripts
"bang path" convention for scripts:
 can invoke Perl at the command line, or
 add #!/public/bin/perl at the beginning of the script
 exact value of path depends upon your platform (use
"which perl" to find the path)
one execution method:
% perl
print "Hello, World!\n";
CTRL-D
Hello, World!

preferred method: set bang-path and ensure


executable flag is set on the script file
Perl Basics

Comment lines begin with: #


File Naming Scheme
 filename.pl (programs)
 filename.pm (modules)
Example prog: print “Hello, World!\n”;
Perl Basics

Statements must end with semicolon


 $a = 0;
Should call exit() function when finished
 Exit value of zero means success
 exit (0); # successful
 Exit value non-zero means failure
 exit (2); # failure
Data Types

Integer
 25 750000 1_000_000_000
 8#100 16#FFFF0000
Floating Point
 1.25 50.0 6.02e23 -1.6E-8
String
 ‘hi there’ “hi there, $name” qq(tin can)
 print “Text Utility, version $ver\n”;
Data Types

Boolean
 0 0.0 “” "0" represent False
 all other values represent True
Variable Types

Scalar
 $num = 14;
 $fullname = “John H. Smith”;
 Variable Names are Case Sensitive
 Underlines Allowed: $Program_Version = 1.0;
Scalars
usage of scalars:
print ("pi is equal to: $pi\n");
print "pi is still equal to: ", $pi, "\n";
$c = $a + $b
important! A scalar variable can be "used" before it is
first assigned a value
 result depends on context
 either a blank string ("") or a zero (0)
 this is a source of very subtle bugs
 if variable name is mispelled — what should be the
result?
 do not let yourself get caught by this – use the "-w"
flag in the bang path:
#!/public/bin/perl -w
Variable Types
List (one-dimensional array)
 @memory = (16, 32, 48, 64);
 @people = (“Alice”, “Alex”, “Albert”);
 First element numbered 0
 Single elements are scalar: $names[0] = “Fred”;
 Slices are ranges of elements
 @guys = @people[1..2];
 How big is my list?
 print “Number of people: “, scalar @people, “ \n”;
Variable Types

Hash (associative array)


 %var = { “name” => “paul”, “age” => 33
};
 Single elements are scalar
 print $var{“name”}; $var{age}++;
 How many elements are in my hash?
 @allkeys
= keys(%var);
 $num = @allkeys;

You might also like