Skip to content
Scalars and Variables
step 1/5

Reading — step 1 of 5

Learn

~1 min readBasics

Perl has three variable sigils (the leading character indicates the type):

  • $ — scalar (single value: number, string, reference)
  • @ — array
  • % — hash (associative array)
my $name = "Ada";
my $age = 36;
my $pi = 3.14159;

my @colors = ("red", "green", "blue");
my %ages = (alice => 30, bob => 25);

Perl is dynamically typed. A scalar can hold a number or string; Perl converts as needed:

my $x = "42";
my $y = $x + 1;          # 43 (numeric context)
my $z = "$x apples";     # "42 apples" (string context)

String interpolation happens in double-quoted strings. Single quotes are literal:

my $name = "Ada";
print "Hello, $name!\n";    # Hello, Ada!
print 'Hello, $name!\n';    # Hello, $name!\n  (literal)

Reading stdin: <STDIN> reads a line including the trailing newline. Use chomp to strip it.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…