Perl Basic Syntax

Essential Perl syntax and program structure

Program Structure

#!/usr/bin/perl # shebang line
use strict; # enable strict mode
use warnings; # enable warnings
print "Hello World\n"; # output to console

Variables

my $name = "John"; # scalar variable
my @array = (1, 2, 3); # array variable
my %hash = (key => "value"); # hash variable
our $global = 10; # global variable

Scalar Data Types

my $number = 42; # integer
my $float = 3.14; # floating point
my $string = "text"; # string
my $undef; # undefined value

String Operations

my $text = "Hello";
length($text); # get length
uc($text); # uppercase
lc($text); # lowercase
"Name: $name"; # string interpolation
'Name: $name'; # no interpolation

Comments

# single line comment
=begin comment
multi-line
comment
=cut