6

Does anyone know, what is the actual reason behind "dollar variable notation" in some of the scripting languages, i.e. PHP or Perl? Python creators did not find $variable useful, neither do I. Why does PHP and Perl force me to press shift-4 so often?

(OK, in Perl you can somehow explain it with distinguishing $scalar, @array and %hash but still it could be successfully avoided there, type does not need to be explicit)

Jakub M.
  • 32,471
  • 48
  • 110
  • 179

4 Answers4

8

I don't know if this is the main reason, but if PHP did not have variables with an identifying marker, the following would not be possible.

echo "Welcome back $name, we last saw you on $date";

Update:

It also creates something called a variable variable possible. The usefulness of a variable variable is debatable though, and I wouldn't recommend anyone making regular use of them.

AndrewR
  • 6,668
  • 1
  • 24
  • 38
  • 1
    It seems that languages as PHP and Perl started out as text processing languages and thus wanted to have quick ways of doing what this example states. Therefore, I think this is the correct answer. – Legolas Aug 03 '11 at 06:04
  • 6
    Perl inherited this syntax from shell scripting, and PHP inherited it from Perl. – mob Aug 03 '11 at 06:07
  • 2
    "the following would not be possible" perhaps it would be better if it weren't. It's the most unreadable feature of any language I know (Brainfuck, Befunge and Malbolge not counting of course). And that would still have been possible without sigils before every variable name. –  Aug 03 '11 at 06:22
  • Why not, `sprintf ` is no worse. (And in some cases much, much better). – Dallaylaen Aug 03 '11 at 07:04
  • @WTP: befunge is quite readable, actually. – ninjalj Aug 03 '11 at 19:32
7

One of the design goals with Perl's variables was to make them visually distinguishable from builtins, reserved words, control flow words, and so on. To someone unfamiliar with Perl, all the clues may look like line noise. But to someone proficient with Perl, the "sigils", as they're called, make code more readable and more understandable. As you already observed, the type of sigil in Perl5 changes depending on the underlying data being accessed. $ for scalar, @ for array, and % for hash.

But there is the additional visual cue that $hash{key} is referring to the scalar value held in the hash. The $ sigil indicates scalar, and the {} brackets indicate that we're indexing into a hash (just as an example). In the case of indexing into an array, $array[0], for example: The $ tells us we're accessing the scalar value held in the array, made obvious by the square brackets, at the index point within the brackets. When we see @array[1, 2, 3], we have an immediate visual notice by way of the @ that we're grabbing a list of elements (assuming a list context) from an array (the square brackets still tell us we're getting them from an array). That's an array slice. A hash slice would be @hash{qw/ this that the other/}. So once again that @ tells us to be looking for a list, and the curly brackets tell us we're getting them from a hash. When we see a @ or a % without postfix brackets, we're taking the entire array or hash.

The cues become second nature quickly.

Perl6 is a little different, and will take proficient Perl5 users a few days to get used to. ;)

There's a good Wikipedia article on the subject here: Sigil (Computer Programming)

Aside from the visual guide, the sigils make other things easier too; dereferencing and interpolation, for example. While more elaborate operators could have been used for something like interpolation (and are still necessary occasionally to disambiguate where a variable ends and adjoining word characters continue), the goal was probably to keep simple things easy, while making hard things possible.

DavidO
  • 13,812
  • 3
  • 38
  • 66
6

Natural Language Principles in Perl:

Part of the reason a language can get away with certain local ambiguities is that other ambiguities are suppressed by various mechanisms. 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
  • 96,171
  • 6
  • 121
  • 214
  • 1
    ...and I see I answered this exactly the same way two years ago in http://stackoverflow.com/questions/1091634/why-do-perl-variables-need-to-start-with – ysth Aug 03 '11 at 16:48
  • @David O, have a twigil, I think I have some to spare – ysth Aug 04 '11 at 02:18
  • That's Perl6, right? Or do we consider the `:` for Perl5 attributes to be a twigil too? – DavidO Aug 04 '11 at 03:36
  • Perl6, yes. The twigil is a second funny character that can come after a sigil, so : doesn't count I think. I think twigils count as adjectives, whereas : is an adverb marker :) – ysth Aug 04 '11 at 03:47
2

They use it when parsing so that variables can easily be distinguished from other stuffs.

kazinix
  • 28,987
  • 33
  • 107
  • 157
  • By "other stuff" you mean keywords probably, but there are maybe 20 keywords so it would be also quite easy to distinguish _them_ from other stuff : ) – Jakub M. Aug 03 '11 at 06:02
  • @JakubM. It depends on the language - perl for e.g. has 260+ keywords, php perhaps less - but there are potential collisions between functions and other symbols (those imported from libraries/modules and the ones you can define). Plus in sigilless languages - a word like `map` or `command` has no explicit meaning (is that a datastructure I've put in a variable? Or a function? Or an shell built-in/executable?) – shalomb Jun 30 '17 at 23:12