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
In Perl, variables are used to store and manipulate data. Depending on the type of data you want to store, Perl provides several types of variables: scalars, arrays, and hashes. This tutorial provides an introduction to these variables and their usage.
Scalars are simple variables. They can hold a single value, which can be a number, a string, or a reference.
Declaration and Assignment
Scalars are prefixed with a $
sign.
my $name = "Alice"; my $age = 30;
Interpolation
Scalars can be interpolated inside double-quoted strings.
print "Hello, $name! You are $age years old.\n";
Arrays in Perl are ordered lists of scalars indexed by numbers.
Declaration and Assignment
Arrays are prefixed with a @
sign.
my @fruits = ("apple", "banana", "cherry");
Accessing Array Elements
Individual array elements can be accessed using their index, starting from 0.
print $fruits[0]; # Outputs "apple"
Adding and Removing Elements
push @fruits, "date"; # Adds "date" to the end of the array my $last_fruit = pop @fruits; # Removes and returns the last element
Hashes (or associative arrays) are sets of key-value pairs, where the keys are unique.
Declaration and Assignment
Hashes are prefixed with a %
sign.
my %person = ( "name" => "Bob", "age" => 40, "city" => "Paris" );
Accessing Hash Values
print $person{"name"}; # Outputs "Bob"
Adding and Removing Key-Value Pairs
$person{"job"} = "Engineer"; # Adds a new key-value pair delete $person{"city"}; # Removes the key-value pair with key "city"
The context in which you use a variable in Perl (scalar or list context) can affect its behavior.
Scalar Context
my $count = @fruits; # $count contains the number of items in @fruits
List Context
my ($first, $second) = @fruits; # $first gets "apple", $second gets "banana"
Perl has many special variables, like $_
, which is the default variable for many operations.
foreach (@fruits) { print $_; # $_ contains the current element in the loop }
In Perl, the scope of a variable (the portion of the code where it can be accessed) can be controlled using my
(lexical scope) or our
(global scope).
my $local_var = "I am local to this block or subroutine."; our $global_var = "I am accessible throughout the package.";
Variables are fundamental to programming in Perl. By understanding scalars, arrays, and hashes, as well as the nuances of context and scope, you can effectively store and manipulate data in your Perl programs.
Scalar variables in Perl examples:
my $name = "Alice"; my $age = 30;
Hash variables in Perl explained:
my %person = ('name' => 'Bob', 'age' => 25);
Perl variable scope and declaration:
my
, our
, local
).sub example_sub { my $local_variable = "Local"; our $global_variable = "Global"; }
Global vs. local variables in Perl:
my $global_variable = "Global"; sub example_sub { my $local_variable = "Local"; }
Perl variable interpolation in strings:
my $name = "Charlie"; my $message = "Hello, $name!";
Dynamic variables in Perl programming:
eval
.my $variable_name = "dynamic_variable"; my $value = 42; eval "\$$variable_name = $value;";
Perl special variables cheat sheet:
$_
, @_
, $!
, etc., have specific meanings.while (<>) { chomp; print "Line: $_\n"; }
Perl variable naming conventions:
my $first_name; my $total_count;