Perl 2
Perl 2
Contents
1 Introduction to Perl 2
5 Operators 5
5.1 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . 5
5.2 String Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.3 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . 6
5.3.1 Numeric Comparisons . . . . . . . . . . . . . . . . . . . . 6
5.3.2 String Comparisons . . . . . . . . . . . . . . . . . . . . . 6
5.4 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
6 Control Structures 6
6.1 Conditionals (if, elsif, else, unless) . . . . . . . . . . . . . . 6
6.2 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6.2.1 for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6.2.2 foreach Loop . . . . . . . . . . . . . . . . . . . . . . . . . 7
6.2.3 while, until . . . . . . . . . . . . . . . . . . . . . . . . . 7
6.2.4 Loop Controls: next, last, redo . . . . . . . . . . . . . . 7
7 Subroutines (Functions) 8
8 I/O Operations 8
8.1 Reading from STDIN . . . . . . . . . . . . . . . . . . . . . . . . . 8
8.2 Writing to STDOUT . . . . . . . . . . . . . . . . . . . . . . . . . 8
8.3 Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . 8
1
9 Regular Expressions (Regex) 9
9.1 Match: = /̃pattern/ . . . . . . . . . . . . . . . . . . . . . . . . . 9
9.2 Substitution: s/// . . . . . . . . . . . . . . . . . . . . . . . . . . 9
9.3 Transliteration: tr/// . . . . . . . . . . . . . . . . . . . . . . . . 10
12 File Handling 12
12.1 Reading from a File . . . . . . . . . . . . . . . . . . . . . . . . . 12
12.2 Writing to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
12.3 Appending to a File . . . . . . . . . . . . . . . . . . . . . . . . . 12
13 Additional Topics 12
13.1 Taint Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
13.2 Special Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
13.3 One-Liners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
14 Best Practices 13
15 Conclusion 14
1 Introduction to Perl
Perl (Practical Extraction and Reporting Language) is a high-level, dynamic
programming language originally developed by Larry Wall in 1987. It excels
at text processing, system administration, and web development, among other
domains. A common Perl motto, "There’s more than one way to do it" (TM-
TOWTDI), reflects the language’s flexibility in problem-solving.
Key features of Perl:
2
2 Setting Up a Perl Environment
Most Unix-like systems (Linux, macOS) come with Perl pre-installed. Check
with:
1 perl -v
• Linux: Use your package manager, e.g., apt-get install perl or yum
install perl.
• macOS: Often already installed; if not, install via brew install perl.
• Windows: Use Strawberry Perl (includes a C compiler) or ActivePerl.
3.1 Comments
Single-line comments use the # symbol:
1 # This is a single-line comment
For multi-line commentary, Perl does not have a native multi-line comment
syntax; use POD (Plain Old Documentation) if extensive commenting is needed.
3
4 Variables and Data Types
Perl has three primary data types, each with its own sigil (prefix):
• Scalars: $variable
• Arrays: @array
• Hashes: %hash
String Interpolation:
1 print "Name: $name\n"; # interpolates $name
2 print ’Name: $name\n’; # does not interpolate
Accessing Elements:
1 print $fruits[0]; # apple
2 print $fruits[1]; # banana
Array Slices:
1 my @subset = @fruits[0,2]; # get elements at indices 0 and 2
4
4.3 Hashes (%)
Hashes store key-value pairs:
1 my %capitals = (
2 "France" => "Paris",
3 "Germany" => "Berlin",
4 "Switzerland" => "Bern"
5 );
Deleting Entries:
1 delete $capitals{"France"};
5 Operators
5.1 Arithmetic Operators
• +, -, *, /, % (modulus)
• ** (exponentiation)
1 my $x = 10;
2 my $y = 3;
3 print $x + $y; # 13
4 print $x % $y; # 1
5 print $x ** $y; # 10^3 = 1000
• Repetition (x):
1 my $repeat = "Ha" x 3; # "HaHaHa"
5
5.3 Comparison Operators
5.3.1 Numeric Comparisons
==, !=, <, >, <=, >=
1 if ($x == $y) {
2 print "Equal numbers\n";
3 }
6 Control Structures
6.1 Conditionals (if, elsif, else, unless)
1 my $age = 18;
2
3 if ($age < 13) {
4 print "Child\n";
5 } elsif ($age < 20) {
6 print "Teen\n";
7 } else {
8 print "Adult\n";
9 }
10
11 # unless is the logical opposite of if
12 unless ($age > 18) {
13 print "You are not older than 18.\n";
14 }
6
6.2 Loops
6.2.1 for Loop
1 my $count = 0;
2 while ($count < 3) {
3 print "Count is $count\n";
4 $count++;
5 }
6
7 my $value = 5;
8 until ($value <= 0) {
9 print "Value is $value\n";
10 $value--;
11 }
7
7 Subroutines (Functions)
1 sub greet {
2 my ($name) = @_; # @_ holds subroutine arguments
3 print "Hello, $name!\n";
4 }
5
6 greet("Alice");
7 greet("Bob");
Returning Values:
1 sub add {
2 my ($a, $b) = @_;
3 return $a + $b;
4 }
5
6 my $sum = add(2, 3); # 5
8 I/O Operations
8.1 Reading from STDIN
Usage:
1 perl script.pl arg1 arg2
8
9 Regular Expressions (Regex)
Perl is famous for its integrated regex support.
9
4 # Global replacement
5 my $text = "apple banana apple pear";
6 $text =~ s/apple/orange/g; # "orange banana orange pear"
1 my $dna = "ATTGCC";
2 $dna =~ tr/ACGT/TGCA/; # complement
3 print $dna; # "TAACGG"
10.2 Dereferencing
1 my @original = @$arr_ref;
2 my $first = $arr_ref->[0]; # array index via ->
3
4 my %orig_hash = %$hash_ref;
5 my $value = $hash_ref->{"a"}; # access by key
10
10.4 Nested Data Structures
1 my $users = {
2 "alice" => {
3 "age" => 25,
4 "email" => "[email protected]"
5 },
6 "bob" => {
7 "age" => 30,
8 "email" => "[email protected]"
9 }
10 };
11
12 print $users->{"alice"}->{"email"}; # "[email protected]"
11.1 CPAN
CPAN (Comprehensive Perl Archive Network) hosts thousands of modules.
Install modules with cpan or cpanm:
11
1 cpanm JSON
12 File Handling
12.1 Reading from a File
1 my $filename = "input.txt";
2 open(my $fh, "<", $filename) or die "Could not open ’$filename’: $!";
3 while (my $line = <$fh>) {
4 chomp($line);
5 print "Line: $line\n";
6 }
7 close($fh);
1 my $out = "output.txt";
2 open(my $fh_out, ">", $out) or die "Could not open ’$out’: $!";
3 print $fh_out "Some text here\n";
4 close($fh_out);
13 Additional Topics
13.1 Taint Mode
Run scripts with -T to enable taint mode for better security (prevents using
untrusted data without sanitization):
1 perl -T script.pl
12
13.2 Special Variables
• $_: Default variable in many operations (e.g., print if /regex/;).
• @_: Holds arguments passed to a subroutine.
• $0: Name of the script.
13.3 One-Liners
Perl is great for command-line one-liners:
1 echo "Hello World" | perl -pe ’s/Hello/Goodbye/’
14 Best Practices
1. Use strict and warnings to catch errors early:
1 use strict;
2 use warnings;
13
15 Conclusion
This tutorial has covered a broad range of Perl topics:
14