27

Why do Perl variables need to start with different characters (sigils)?

  • Scalar variables start with $

  • Hashes start with %

  • Arrays start with @

Why are they like this?

benrifkah
  • 1,526
  • 14
  • 31
joe
  • 34,529
  • 29
  • 100
  • 137
  • 4
    Short answer: they don’t. Consider arrays and hashes. That said, the question is confusing. Care to clarify? – Konrad Rudolph Jul 07 '09 at 10:50
  • 1
    Are you asking why these particular sigils have been chosen (as opposed to three different ones)? Or why there are sigils at all? – Thilo Jul 07 '09 at 10:54
  • I asked with about only sigils , then i realized my self why other varialble also have like that . so i asked about all ? – joe Jul 07 '09 at 10:56
  • Untangled: *"I only asked about sigils, but then I realized why other variables also have it like that. So I asked about all."* Though it still doesn't make much sense. – Peter Mortensen Feb 09 '22 at 17:19

6 Answers6

67

When I started out using Perl it was explained to me that these characters were chosen because:

  • $ looked a bit like an 's' so that was for scalars,
  • @ has an 'a' in the middle so that was for arrays, and
  • % was for hashes because it looked like a key-value pair divided by a slash.
George Stocker
  • 57,289
  • 29
  • 176
  • 237
Rob Wells
  • 36,220
  • 13
  • 81
  • 146
  • 9
    Yes, the Camel book tells the same story. Or at least, it doesn't say they were actually chosen because of those reasons, but that you could remember better what they stand for with those mnemonics. – Adriano Varoli Piazza Jul 07 '09 at 13:46
  • 3
    I also think % looks a little like a "H" for hash if you use a lot of poetic license. – Matthew Lock Jul 06 '15 at 00:35
36

This is because Perl uses sigils:

In computer programming, a sigil (pronounced /'sɪdʒ.ɪl/ or /'sɪg.ɪl/; plural sigilia or sigils) is a symbol attached to a variable name, showing the variable's datatype or scope. The term was first applied to Perl usage by Philip Gwyn in 1999 to replace the more cumbersome "funny character in front of a variable name". The name is based on the word meaning a magical symbol (see sigil (magic)).

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
23

Several reasons are explained by Larry Wall et al in "Programming Perl":

Within any given namespace [...] every variable type has its own subnamespace, determined by the funny character. You can, without fear of conflict, use the same name for a scalar variable, an array, or a hash (or, for that matter, a filehandle, a subroutine matter, a label or your pet llama.)

[...]

Like most computer languages, Perl has a list of reserved words that it recognizes as special keywords. However, because variable names always start with a funny character, reserved words don't actually conflict with variable names.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Leonardo Herrera
  • 8,388
  • 5
  • 36
  • 66
  • 4
    This makes Perl a Lisp 4 (or 5 if you count the *GLOB) – dsm Jul 07 '09 at 15:09
  • 5
    Namespace protection is a major benefit of sigils, IMO. ++! – daotoad Jul 07 '09 at 19:34
  • 3
    Doesn't the fact that you can also use variables in different "contexts" make things terribly confusing? Doesn't this make using the same name for different variables impossible? – temp2290 Jan 29 '10 at 16:43
  • 1
    @temp2290 No. Consider `@arr` and `$arr`, they are two different variables. When, for example, you are using `@arr` in a scalar context, you are either accessing a single element: `my $element = $arr[1]` or the length of the array: `my $length = @arr;`. It both cases they are easily distinguished from the scalar variable `$arr`. If `$arr` was a scalar reference to an array, you could do `my $element = $arr->[0];` and `$length = @$arr;`. IMHO at least those are all easily distinguished. – cftarnas Aug 04 '11 at 05:07
  • 5
    @cftarnas: I'm hoping that there are multiple errors in your comment rather than that being the correct usage of the language. I don't really know because I am just starting learn Perl. Anyway, if your goal was to demonstrate that Perl's use of sigils is not confusing, I don't think you have succeeded. Perhaps people who have *already* master Perl would disagree with me. – Brent Bradburn Oct 12 '15 at 04:32
  • 1
    @nobar Perl is confusing at the beginning, no discussion. – Leonardo Herrera Oct 12 '15 at 14:32
12

From Natural Language Principles in Perl:

English uses number and word order, with vestiges of a case system in the pronouns: "The man looked at the men, and they looked back at him." It's perfectly clear in that sentence who is doing what to whom. Similarly, Perl has number markers on its nouns; that is, $dog is one pooch, and @dog is (potentially) many. So $ and @ are a little like "this" and "these" in English.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ysth
  • 96,171
  • 6
  • 121
  • 214
11
  • Because Perl was intended to replace shell scripts, and variables in shell start with $.
  • To distinguish between scalars ($), arrays (@) and hashes (%).
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dave4420
  • 46,404
  • 6
  • 118
  • 152
  • I wouldn't say perl was intended to replace shell scripts and so kept the `$` sigil (POSIX shell variable are actually initialized without a `$`) - the benefit of the `$` sigil is you can use/interpolate variables without too much context-switching trickery in your code e.g. `echo "the value of variable foo is $foo"` (as opposed to `System.out.println("the value of foo is" + foo)` and that's as far as perl and shell share the commonality. Perl's `$` sigil is more to your second point (denoting variable type/use). – shalomb Jan 16 '16 at 14:26
2

Not all of them do. Some start with % (hashes) or with @ (arrays).

It is a design decision to mark them as variables and also denote their type.

Note that you can have both a $abc and a %abc.

Check out a tutorial on Perl variables.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    The sigils don't denote type. They denote how you are treating the thingy. For example, $abc[1] is still using an array variable even though there is a $ out front. – brian d foy Jul 15 '09 at 23:34
  • That inconsistency is going to be fixed in Perl6, where you write @abc[1] to access the second element in the array @abc. – Thilo Jul 16 '09 at 02:26
  • Assuming any of us live long enough to see a full release of Perl 6. – David Thornley Sep 01 '09 at 21:05
  • @David: a valid concern. I think Ray Kurzweil is working on that problem. ;-) – Thilo Sep 02 '09 at 00:25
  • @David Thornley: Hopefully you still be around come Spring 2010 then! (http://www.h-online.com/open/Perl-6-due-in-spring-2010-some-of-it-anyway--/news/113956) – draegtun Sep 05 '09 at 18:47
  • @Thilo: Its not an inconsistency though. However Larry Wall conceded that it was confusing to "beginners" hence changed its semantics in Perl6. – draegtun Sep 05 '09 at 18:55