Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Perl is a versatile and dynamic scripting language that has been popular for text processing, system administration, and web development. Its syntax is unique and allows for more than one way to do things, leading to its motto: "There's more than one way to do it."
In this tutorial, we will take a look at the basic syntax of a Perl program.
The first line of a Perl script usually starts with a shebang (#!
). This tells the system that this file should be executed using Perl.
#!/usr/bin/perl
For more portability across systems, especially if Perl might reside in a different directory, you can use:
#!/usr/bin/env perl
Comments in Perl start with a #
symbol and extend to the end of the line.
# This is a comment print "Hello, World!\n"; # This is also a comment
Perl statements end with a semicolon.
print "Hello, World!\n"; print "Perl is fun.\n";
Scalar Variables: Store single values (numbers, strings). They start with a $
sign.
my $name = "Alice"; my $age = 30;
Arrays: Store ordered lists of scalars and start with an @
sign.
my @fruits = ("apple", "banana", "cherry");
Hashes (or Associative Arrays): Store key-value pairs and start with a %
sign.
my %data = ("John", 25, "Jane", 30);
Conditional Statements:
if ($age > 18) { print "Adult\n"; } elsif ($age == 18) { print "Just turned 18!\n"; } else { print "Not an adult.\n"; }
Loops:
# While loop while ($count < 10) { print $count, "\n"; $count++; } # For loop for my $i (0..9) { print $i, "\n"; }
Declare with sub
and then the subroutine name.
sub greet { my ($name) = @_; print "Hello, $name!\n"; } greet("Bob"); # Calls the subroutine with "Bob" as argument
You can use external modules or libraries using the use
statement.
use strict; # Helps write better code by enforcing certain good practices use warnings; # Enables warning messages
Perl has many built-in special variables. For instance, $_
is the default variable and many built-in functions operate on $_
if no variable is given.
$_ = "Hello"; print if /Hello/; # Prints "Hello" because $_ contains "Hello"
To run a Perl program named script.pl
:
perl script.pl
The syntax of Perl is rich and flexible, allowing for concise and expressive programs. Familiarizing yourself with the basics, as described above, provides a foundation for diving deeper into Perl programming and leveraging its full potential.
Perl script structure and organization:
#!/usr/bin/perl use strict; use warnings; # Perl script structure and organization sub main { print "Hello, Perl!\n"; } main(); # Call the main subroutine
Perl script execution and entry point:
#!/usr/bin/perl use strict; use warnings; # Perl script execution and entry point sub main { print "Executing main subroutine!\n"; } main(); # Entry point for script execution
Looping constructs in Perl script:
for
and while
in Perl for repetitive tasks.#!/usr/bin/perl use strict; use warnings; # Looping constructs in Perl script sub for_loop_example { for my $i (1..5) { print "Iteration $i\n"; } } sub while_loop_example { my $count = 1; while ($count <= 5) { print "Count: $count\n"; $count++; } } for_loop_example(); while_loop_example();