HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PerlOperatorsStringsArrays (@)Hashes (%)Conditions (if)LoopsFunctions (sub)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
✍️ Text & Number
📋 Array & Hash
🔁 Condition & Loop
🧩 Function (sub)
Perl›Arrays (@)

Arrays (@)

Arrays with @, adding items with push, counting with scalar, and iterating with foreach.

Simple enough for a 5-year-old to understand

Create an array and access elements

Example code
1
my @colors = ("red", "purple");
@ = array.
Data@ = array.
2
push @colors, "green";
Add to the end.
CodeAdd to the end.
3
print "$colors[0]\n";
First element: get ($ sigil!)
Output / InputFirst element: get ($ sigil!)
4
print scalar(@colors), "\n";
Number of elements: 3
Output / InputNumber of elements: 3
DataCodeOutput / Input
💡 Hint: The only element of the array is $arr[0] ($ sigil). The number of elements is scalar(@arr).

About this topic

In Perl, arrays are denoted with names starting with an at sign and hold ordered scalar values. You access the entire array with an at sign, and a single element with a dollar sign — this change of sigil is unique to Perl and is the rule that surprises beginners the most.

This rule is actually consistent: the sigil tells you the type of thing you are accessing. If you want the whole array, you use an at sign; if you want a single scalar, you use a dollar sign. Since the element itself is a scalar, the dollar sign is correct.

Arrays in Perl are dynamic; you don't need to declare a size, and they can grow and shrink whenever you want. There are four separate functions that work at the beginning and the end for insertion and removal operations.

What you will learn

  • Defining arrays and accessing elements
  • The sigil change rule
  • Functions for adding and removing elements
  • Learning the array length
  • Iterating over an array

Who is this for?

  • Beginners to Perl
  • Those who make mistakes with the sigil rule
  • Those who will process data in list form

When to use?

Lines read from a file, command output fragments, and argument lists are all processed as arrays.

If key-based access is required, a hash should be used instead of an array; searching in an array is slow for large data.

Common mistakes

Wrong

my @liste = (1, 2, 3);
print @liste[0];

A dollar sign must be used when accessing a single element; an at sign means a slice, and this code produces a warning. The rule is: if the thing you are accessing is a single scalar, use a dollar sign; if it is multiple elements, use an at sign.

Correct

my @liste = (1, 2, 3);
print $liste[0];

Wrong

my @liste = (1, 2, 3);
my $uzunluk = @liste[0];
print "Length: $uzunluk\n";

This expression gives the first element as a slice, not the length. To get the number of elements in the array, you need to use the array in scalar context; in Perl, context allows the same expression to take on different meanings, and this concept is unique to the language.

Correct

my @liste = (1, 2, 3);
my $uzunluk = scalar(@liste);
print "Length: $uzunluk\n";

Wrong

my @liste = (1, 2, 3);
print $liste[10];

An out-of-bounds index does not throw an error in Perl; it returns an undefined value and nothing is printed to the screen. If warnings are enabled, you get a "Use of uninitialized value" warning. This silent behavior leads to bugs being noticed much later.

Correct

my @liste = (1, 2, 3);
my $i = 10;
if ($i <= $#liste) { print $liste[$i]; } else { print "out of range\n"; }

Frequently asked questions

How do I find out the last index?

The special variable in the form of dollar-hash gives the last index of the array; it is one less than the number of elements. If you want the number of elements, using the array in scalar context is clearer.

What do push, pop, shift, and unshift do?

push adds to the end, pop removes from the end; unshift adds to the beginning, shift removes from the beginning. Operations at the beginning shift all elements, so they are slower on large arrays compared to operations at the end.

How do I sort the array?

The sort function performs alphabetical sorting by default; to sort numbers correctly, you need to provide a comparison block. Those who don't know the default behavior see unexpected order in a list of numbers.

Summary

The whole array is denoted with an at sign, and a single element with a dollar sign.

The number of elements is obtained in scalar context.

Out-of-bounds access returns an undefined value.

There are four separate functions for adding and removing.

The default sorting is lexicographical.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code