The Sigils
In Perl, the first character of a variable name (the Sigil) tells you what kind of data it holds. This is one of the most distinctive features of the language.
| Sigil | Type | Description |
|---|---|---|
| $ | Scalar | A single value (Number, String, or Reference) |
| @ | Array | A list of scalar values |
| % | Hash | A collection of key-value pairs |
1. Scalars ($)
A scalar holds a single piece of information.
perl
2. Arrays (@)
Arrays are ordered lists. They are 0-indexed.
perl
Wait! Notice that when you access a single item from an array, you use the
$ sigil because the result is a scalar.3. Hashes (%)
Hashes are key-value pairs (also called associative arrays).
perl
Context Matters
Wait! Perl is famous for Context. A variable might behave differently depending on whether it is used in a "Scalar Context" (expecting one value) or a "List Context" (expecting many).
perl
Summary
| Term | Description |
|---|---|
| my | Declares a local variable (Lexical scope) |
| $ | Single value (Scalar) |
| @ | Ordered list (Array) |
| % | Key-Value pairs (Hash) |
| qw() | "Quote Words" - Shorthand for an array: qw(a b c) |
| Best For | Organizing and manipulating structured text data |
| Logic | "The prefix defines the type" |
| Safety | Always use my to avoid global variable leaks |
| Modern | Modern Perl uses my exclusively |
| Standard | Sigils make variables easy to spot in complex text code |
| Identity | The sigils originated from the shell scripting world |