Dollar notation in script languages ​​- why?

Does anyone know what the actual reason for “dollar variable notation” is in some scripting languages, ie PHP or Perl? The creators of Python did not find $variable useful, nor did I. Why do PHP and Perl make me press shift-4 so often?

(OK, in Perl you can somehow explain this by distinguishing between $scalar , @array and %hash , but you could successfully avoid it, the type need not be explicit)

+6
source share
4 answers

I do not 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 a possible variable. The usefulness of a variable variable is debatable, though, and I would not recommend anyone make regular use of them.

+8
source

One of the goals of design with Perl variables was to make them visually distinguishable from embedded, reserved words, control flow words, etc. For someone unfamiliar with Perl, all the hints may look like linear noise. But for someone who owns Perl, the sigils, as they are called, make the code more understandable and understandable. As you already noticed, the sigil type in Perl5 changes depending on access to the underlying data. $ for scalar, @ for array and% for hash.

But there is an additional visual cue that $hash{key} refers to the scalar value stored in the hash. The $ symbol indicates a scalar, and the brackets {} indicate that we are indexing the hash (as an example). In the case of indexing into an array, $array[0] , for example: $ tells us that we are accessing the scalar value stored in the array, which is obvious in square brackets, at the index point in brackets. When we see @array[1, 2, 3] , we have a direct visual notification with @ that we grab a list of elements (subject to the context of the list) from the array (square brackets still tell us that we get them from the array ) This is a slice of an array. The hash slice will be @hash{qw/ this that the other/} . So again, what @ tells us to look for a list, and curly braces tell us that we get them from the hash. When we see @ or % without postfix brackets, we take the whole array or hash.

Signals become second nature fast.

Perl6 is a little different, and in a few days the user will use experienced Perl5 users.;)

There is a good Wikipedia article on the topic: Sigil (computer programming)

Besides visual guidance, sigils facilitate other things; dereferencing and interpolation, for example. Although more complex operators could be used for something like interpolation (and it is still necessary to disambiguate from time to time when the variable ends and adjoining word-characters continue), the goal was probably to simplify simple things by doing everything possible.

+7
source

Natural Language Principles in Perl :

Part of the reason why a language can get away from some local ambiguities is because other ambiguities are suppressed by various mechanisms. English uses the number and order of words, with the remnants of the case system in the pronouns: "The man looked at the men and they looked back at him." In this sentence, it is clear who does what, to whom. Similarly, Perl has numeric markers in its nouns; that is, $dog is one pooch, and @dog (potentially) many. So $ and @ bit like "this" and "these" in English.

+6
source

They use it in parsing so that the variables are easily different from others.

+2
source

Source: https://habr.com/ru/post/894160/


All Articles