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
Scalars are one of the fundamental data types in Perl, alongside arrays and hashes. Let's go over scalars in detail.
In Perl, a scalar represents a single value. This can be a number, a string, or a reference. Unlike some languages where you need different types for integers, floating-point numbers, and characters, Perl scalars are versatile and can hold any of those types.
Scalar variables start with the $
symbol followed by the variable name.
my $name = "Alice"; my $age = 25; my $pi = 3.14159265;
Integers and floating-point numbers are both represented as scalars.
Perl automatically converts between numbers and strings as necessary. This feature is known as type coercion.
my $result = "4" + "5"; # $result is 9
Numeric operations on non-numeric strings default to 0.
my $n = "hello" + 2; # $n is 2
You can define string scalars using either single (' '
) or double (" "
) quotes.
Interpolation happens in double-quoted strings.
my $name = "Bob"; print "Hello, $name\n"; # Outputs: Hello, Bob
In single-quoted strings, it won't interpolate:
print 'Hello, $name\n'; # Outputs: Hello, $name\n
You can concatenate strings using the .
operator.
my $full_name = "Alice" . " " . "Cooper";
A scalar variable that has not been assigned a value yet is undefined
. This is represented by the special value undef
in Perl.
my $unknown; print $unknown; # Outputs nothing and raises a warning if 'use warnings;' is active.
undef
You can check if a scalar is defined using the defined
function.
print "defined\n" if defined $unknown; # This won't print
Assigning undef
to a scalar clears its value.
$name = undef;
Many Perl functions and operations change behavior based on context. In a scalar context, they return a single value.
my $count = @array; # $count gets the number of elements in @array because of the scalar context
Perl offers a number of functions that operate specifically on scalars:
chomp
: Removes trailing newline from a string if present.chop
: Removes the last character from a string.length
: Returns the length of a string.lc
: Returns the string with all characters in lowercase.uc
: Returns the string with all characters in uppercase.my $text = "HELLO"; print lc($text); # Outputs: hello
In Perl, scalars are versatile single-value containers that can hold strings, numbers, or references. They are a foundational component of the language, and understanding how to work with them is crucial for any Perl programmer.
Working with scalar variables in Perl:
my $name = "John"; my $age = 25; print "Name: $name, Age: $age\n";
Scalar data type in Perl:
my $salary = 50000; print "Salary: $salary\n";
Perl scalar initialization and assignment:
my
and assigned values using the assignment operator (=
).my $city; # Scalar initialization $city = "New York"; # Scalar assignment print "City: $city\n";
Scalar operations and expressions in Perl:
my $num1 = 10; my $num2 = 5; my $sum = $num1 + $num2; my $concatenation = "Hello" . " " . "World"; print "Sum: $sum, Concatenation: $concatenation\n";
Scalars and context in Perl:
my $count = ("apple", "orange", "banana"); print "Scalar in list context: $count\n";
Scalar vs. list context in Perl:
my @fruits = ("apple", "orange", "banana"); my $count = @fruits; print "Scalar context: $count\n";
Scalar interpolation in Perl:
$
sigil.my $name = "Alice"; print "Hello, $name!\n";