Perl Programming – Quick Review (Simple & Easy Guide)

perl programming

Perl is a powerful, flexible, and beginner‑friendly scripting language. It is famous for text processing, regular expressions, system administration, and web development.

This guide explains all important Perl topics in simple English, with easy examples, perfect for students, beginners, and interview preparation.

1. What is Perl?

Perl is a high‑level programming language created by Larry Wall. It is often called the “Swiss Army Knife” of programming because it can do many tasks easily.

Where Perl is used

  • Text processing
  • Log file analysis
  • System administration
  • Web development (CGI)
  • Automation scripts

2. Perl Program Structure

A simple Perl program looks like this:

#!/usr/bin/perl
use strict;
use warnings;

print "Hello, World!\n";

Explanation

  • #!/usr/bin/perl → tells system to use Perl
  • use strict; → forces good coding rules
  • use warnings; → shows warning messages
  • print → prints output

3. Variables in Perl

Perl uses symbols to identify variable types.

SymbolTypeExample
$Scalar (single value)$age = 25;
@Array (list)@nums = (1,2,3);
%Hash (key‑value)%marks = (Math => 90);

Scalar Variable Example

my $name = "Krishna";
my $age = 22;
print "$name is $age years old\n";

4. Data Types

4.1 Scalars

Store single values (number or string).

my $price = 100;
my $city = "Chennai";

4.2 Arrays

Store multiple values.

my @colors = ("Red", "Green", "Blue");
print $colors[0];   # Red

4.3 Hashes

Store key‑value pairs.

my %student = (
  name => "Raj",
  age  => 21
);
print $student{name};

5. Operators

Arithmetic Operators

my $a = 10;
my $b = 5;
print $a + $b;   # 15

Comparison Operators

==   equal
!=   not equal
>    greater than
<    less than

String Comparison

 eq   equal
 ne   not equal

6. Conditional Statements

if Statement

my $age = 18;
if ($age >= 18) {
    print "Eligible to vote";
}

if‑else

if ($age >= 18) {
    print "Adult";
} else {
    print "Minor";
}

7. Loops in Perl

for Loop

for (my $i = 1; $i <= 5; $i++) {
    print "$i\n";
}

while Loop

my $i = 1;
while ($i <= 3) {
    print "$i\n";
    $i++;
}

foreach Loop

my @nums = (10, 20, 30);
foreach my $n (@nums) {
    print "$n\n";
}

8. Subroutines (Functions)

Subroutines are reusable blocks of code.

sub add {
    my ($a, $b) = @_;
    return $a + $b;
}

print add(5, 3);

9. Strings in Perl

String Concatenation

my $first = "Hello";
my $second = "World";
print $first . " " . $second;

String Length

print length("Perl");

10. Arrays – Important Functions

my @arr = (1,2,3);
push @arr, 4;    # add
pop @arr;        # remove last
shift @arr;      # remove first
unshift @arr, 0; # add first

11. Hash Functions

my %data = (a => 1, b => 2);

my @keys = keys %data;
my @values = values %data;

12. File Handling in Perl

Writing to a File

open(my $fh, '>', 'test.txt');
print $fh "Hello Perl";
close($fh);

Reading from a File

open(my $fh, '<', 'test.txt');
while (<$fh>) {
    print $_;
}
close($fh);

13. Regular Expressions (Regex)

Perl is very famous for regex.

my $text = "I love Perl";
if ($text =~ /Perl/) {
    print "Found";
}

14. Command Line Arguments

my $name = $ARGV[0];
print "Hello $name";

Run:

perl test.pl Krishna

15. Modules in Perl

Modules help reuse code.

use Math::BigInt;

Install module:

cpan Module::Name

16. Object‑Oriented Perl (Basic)

package Person;

sub new {
    my $class = shift;
    return bless {}, $class;
}

1;

17. Advantages of Perl

  • Very powerful text processing
  • Easy scripting
  • Huge library (CPAN)
  • Cross‑platform

18. Disadvantages of Perl

  • Code readability can be difficult
  • Less popular today compared to Python

19. Perl vs Other Languages

FeaturePerlPython
Text ProcessingExcellentGood
ReadabilityMediumVery Good
SpeedFastFast

20. Error Handling in Perl

Error handling helps us find and fix problems in a program.

die Function

open(my $fh, '<', 'nofile.txt') or die "File not found";

If the file does not exist, the program stops and shows the message.

warn Function

warn "This is a warning message";

warn shows a message but does not stop the program.

21. Input from User

Perl can take input from the user using STDIN.

print "Enter your name: ";
my $name = <STDIN>;
chomp($name);
print "Hello $name";

chomp removes the extra newline character.

22. Date and Time in Perl

my $time = localtime();
print "Current time: $time";

Perl can easily handle system date and time.

23. Useful Built‑in Functions

Some commonly used Perl functions:

print uc("perl");     # PERL
print lc("PERL");     # perl
print reverse("abc"); # cba

24. Perl for System Administration

Perl is widely used by system admins.

Example: Run System Command

my $files = `ls`;
print $files;

This command lists files in the current directory.

25. Web Programming with Perl (Basic Idea)

Perl is used in CGI scripts for web applications.

print "Content-type: text/html

";
print "<h1>Hello from Perl</h1>";

26. Interview Important Points

  • Perl variables use symbols $ @ %
  • Perl is best known for regular expressions
  • CPAN is Perl’s module repository
  • strict and warnings improve code quality

27. Real‑World Use Cases of Perl

  • Parsing log files
  • Data extraction
  • Automation scripts
  • Report generation
  • Bioinformatics

28. Best Practices in Perl

  • Always use strict and warnings
  • Write readable code
  • Use comments for clarity
  • Reuse code using modules

29. Learning Path for Beginners

  1. Learn Perl syntax
  2. Practice arrays and hashes
  3. Master regex
  4. Learn file handling
  5. Explore CPAN modules

30. Conclusion

Perl is a mature, stable, and powerful scripting language. Even today, it is widely used for text processing, automation, and system tasks. Its strong regular expression support makes it unique compared to other languages.

For beginners, Perl builds a strong foundation in scripting and problem solving. For professionals, it remains a reliable tool for real‑world automation.

If you are preparing for interviews, exams, or practical projects, learning Perl is still a valuable skill.

Happy Coding with Perl 🚀


Recommended Topics

Leave a Reply

Your email address will not be published. Required fields are marked *