perl primer
DESCRIPTION
TRANSCRIPT
![Page 2: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/2.jpg)
History & Roadmap
● Created by Larry Wall● Powerful text handling● Evolved with the Internet● CGI / mod_perl● OOP● Matured as a serious platform for development● Perl 6 / Parrot
![Page 3: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/3.jpg)
Key Features
● Simple, intuitive, versatile syntax● Cross platform● Regular expression support● Supports OOP semantics● Open source – dual licensed● Thriving community● CPAN
![Page 4: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/4.jpg)
Supported Platforms
Linux, AIX, IRIX, HP/UX, BSD, Solaris, Tru64, DOS, Windows 3.1, Acorn Risc OS,
Windows 95/98/NT/2000/XP, Apple Macintosh, Amiga, BeOS, OS/2, AS/400, OS390, VMS,
OpenVMS, Stratus, Tandem, EPOC, QNX, Plan 9, Atari ST, GNU HURD, Cygwin, HP 3000
MPE etc.
![Page 5: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/5.jpg)
PracticalExtraction &ReportingLanguage
![Page 6: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/6.jpg)
PathologicallyEclecticRubbishLister
![Page 7: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/7.jpg)
There is More Than One Way To Do It
TMTOWDI(timtowdi)
![Page 8: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/8.jpg)
Hello World
print “Hello World!\n”;
![Page 9: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/9.jpg)
Hello World
perl e 'print “Hello World!\n”;'
![Page 10: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/10.jpg)
Hello World
echo 'print “Hello World!\n”' | perl
![Page 11: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/11.jpg)
Running the First Script
File: HelloWorld.pl#!/usr/bin/perlprint “Greetings, Earth People!\n”;
$ perl HelloWorld.pl or$ perl < HelloWorld.pl or$ chmod a+x HelloWorld.pl$ ./HelloWorld.pl
![Page 12: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/12.jpg)
Program Structure
● Lenient structure.● No indentation, whitespace rules.● Every simple statement ends in a semicolon.● All user created objects, except subroutines, are automatically created with a null or zero value.● Comments are prefixed with a #● Lines starting with = are interpreted as the start of a section of embedded documentation
![Page 13: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/13.jpg)
Data Types
● Scalars
● Arrays
● Hashes
![Page 14: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/14.jpg)
Scalars
● Simple variables● Preceded by a $● Loosely typed
$var = “random string”;$var = “with \n special \t characters”;$var = 23;$var = 15.3439;$var = 0xff; # Hexadecimal$var = 033; # Octal
![Page 15: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/15.jpg)
Arrays
● Ordered list of scalars● Preceded by @● Elements are accessed by subscript● Elements are numbered starting from 0.● Negative indexes count backwards
@array = (1, 1, 2, 3, 5, 8, 13);print $array[4]; # Prints 5
![Page 16: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/16.jpg)
Hashes
● Unordered set of key/value pairs● Preceded by %● Keys are used as subscripts to access elements
%lives = ( “cat” => 9, “dog” => 1.5);%lives = (“cat”, 9, “dog”, 1.5);$lives{'cat'} # Returns 9$lives{'dog'} = 2;
![Page 17: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/17.jpg)
Conditionals
if (expression) {block} else {block}
unless (expression) {block} else {block}
if (expression1) {block}elsif (expression2) {block}elsif (lastexpression) {block}else {block}
![Page 18: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/18.jpg)
Loops
while (<INFILE>) { print OUTFILE, "$_\n";}
for ($i = 1; $i < 10; $i++) { ... # Do something}
![Page 19: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/19.jpg)
Loops
foreach var (list) { ...}
● Can be used to iterate through hashes and arrays
foreach $element (@array) {print “$element\n”;
}
![Page 20: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/20.jpg)
Modifiers
statement if EXPR;statement unless EXPR;statement while EXPR;statement until EXPR;
$i = $num if ($num < 50); # $i will be less than 50$j = $cnt unless ($cnt < 100); # $j will be >= 100$lines++ while <FILE>;print "$_\n" until /The end/;
![Page 21: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/21.jpg)
Modifiers
do { $line = <STDIN>; ...} until $line eq ".\n";
do { $line = <STDIN>; ...} while length ($line) > 1;
![Page 22: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/22.jpg)
Loop Control
LINE: while (<SCRIPT>) { print; next LINE if /^#/; # discard comments}
last labelnext labelredo label
![Page 23: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/23.jpg)
Special Variables
● Perl has many special variables
● $_ implicit assignmentforeach ('hickory','dickory','doc') {
print; # Prints $_}
![Page 24: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/24.jpg)
Special Arrays and Hashes
● @ARGV – Command line arguments passed into the script● @INC – List of places to look for scripts and modules● %ENV – Hash containing the environment the script is running in
![Page 25: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/25.jpg)
Special Filehandles
● STDIN
● STDOUT
● STDERR
![Page 26: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/26.jpg)
Subroutines
sub name {block}sub name (prototype) {block}
name(args); # & is optional with parenthesesname args; # Parens optional if predeclared&name; # Passes current @_ to subroutine
● The implicit return value is the result of the last evaluated statement
![Page 27: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/27.jpg)
Switches
● perl n assumes awhile (<>) {
... # your script goes here}
around the command line arguments
● perl p does the same while printing the line
Example: perl ne “print if /PATTERN/” filename
![Page 28: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/28.jpg)
More Switches
● To search and replace text in a file:
perl i.bak p e 's/match/replace/g' filename
● i switch backs up modified files with a .bak extension.
● See perl –help for more switches
![Page 29: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/29.jpg)
Installing Modules
Download foomodule.tar.gz$ tar zxvf foomodule.tar.gz$ cd foomodule$ perl Configure.PL$ make$ make test# make install ORuse CPAN.
![Page 30: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/30.jpg)
CPAN
● CPAN.org● Comprehensive Perl Archive Network● Mirrors all over the world● Command line shell● Bundled with standard Perl distribution● Intuitive module management
![Page 31: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/31.jpg)
CPAN
perl MCPAN e shell
cpan> install Term::ReadKeycpan> install Term::ReadLinecpan> install Bundle::CPANcpan> h or ?
![Page 32: Perl Primer](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b7a8404a7959b0218b45ef/html5/thumbnails/32.jpg)
Thank You!