tags:

views:

515

answers:

6

Why do Perl variables need to start with different characters?

  • Scalar variables start with $

  • Hashes start with %

  • Arrays start with @

Why are they like this?

+25  A: 

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
+6  A: 
  • Because Perl was intended to replace shell scripts, and variables in shell start with $.
  • To distinguish between scalars ($), arrays (@) and hashes (%).
Dave Hinton
+2  A: 

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.

Thilo
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
That inconsistency is going to be fixed in Perl6, where you write @abc[1] to access the second element in the array @abc.
Thilo
Assuming any of us live long enough to see a full release of Perl 6.
David Thornley
@David: a valid concern. I think Ray Kurzweil is working on that problem. ;-)
Thilo
@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
@Thilo: Its not an inconsistency though. However Larry Wall conceded that it was confusing to "beginners" hence changed its semantics in Perl6.
draegtun
+24  A: 

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.
Rob Wells
Interesting Answer :-)
joe
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
+9  A: 

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.

Leonardo Herrera
This makes Perl a Lisp 4 (or 5 if you count the *GLOB)
dsm
Namespace protection is a major benefit of sigils, IMO. ++!
daotoad
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
+6  A: 

http://www.wall.org/~larry/natural.html:

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.

ysth