Dollar notation in script languages - why? [closed]
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this questionDoes 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 distinguishi开发者_如何学Gong $scalar
, @array
and %hash
but still it could be successfully avoided there, type does not need to be explicit)
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.
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.
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.
They use it when parsing so that variables can easily be distinguished from other stuffs.
精彩评论