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)
The $x (single dollar) is the normal variable with the name x that stores any value like string, integer, float, etc. The $$x (double dollar) is a reference variable that stores the value which can be accessed by using the $ symbol before the $x value. These are called variable variables in PHP.
$ is the way to refer to variables in PHP. Variables in PHP are dynamically typed, which means that their type is determined by what's assigned to them. Here's the page about variables from the PHP manual. $a = "This is a string"; $b = 1; // This is an int.
Rules for PHP variables: A variable starts with the $ sign, followed by the name of the variable. A variable name must start with a letter or the underscore character. A variable name cannot start with a number.
The @ symbol is the error control operator ("silence" or "shut-up" operator). It makes PHP suppress any error messages (notice, warning, fatal, etc) generated by the associated expression. It works just like a unary operator, for example, it has a precedence and associativity.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With