Understanding Scalars: Strings and Numbers

In Perl, variables are prefixed with special characters called sigils. These sigils tell you (and the compiler) what kind of container you are working with.

The most basic container is the Scalar, prefixed with the dollar sign ($) (think $for Singular).

Declaring Scalars

A scalar can hold a single item of information: a text string, an integer, or a floating-point decimal number. Perl manages memory automatically and dynamically casts types for you.

use strict;
use warnings;

my $name = "Vax";          # A text string
my $age = 29;              # An integer
my $pi = 3.14159;          # A float decimal

Single Quotes vs. Double Quotes

The type of quotation marks you use for text variables dictates how Perl processes them:

my $greeting = "Hello, $name\n"; # Outputs: Hello, Vax (with a new line)
my $literal  = 'Hello, $name\n'; # Outputs: Hello, $name\n
Next Module ->