Four basic data types in Perl (as far as I know): Scalar, array, list, and hash.
Scalar should be the atomic Data Structure in Perl, which cannot be further decomposed by programmers, such as int and double in C language. For scalar, I have the following views:
The name "scalar" is very strange. It has a very "physical" and "Mathematical" feeling, and it makes beginners think there must be a data type called "vector. In fact, scalar is a common data type (which has a mathematical concept and has nothing to do with physics). Perl does not have a basic data type called "vector" or "vector. I really don't know. What did larry think? It would be called.
A scalar includes a numeric value and a string, which are converted to each other as needed. For example, when $ V1 + $ V2 is used, the two scalar values are automatically converted to numerical values. The conversion process is automatically performed. You do not need to display instructions. You are not sure whether the conversion syntax is displayed. There are certain rules for the conversion process. For example, if $ V1 is "world" and $ V1 is converted to 0, there are more than one rule. If it is $ V1. $ V2, the string is spliced. The conversion here is very understandable. In many cases, you are not allowed to consider whether it is a numeric value or a string, because most of the time it is processing a string and when you need to process a numeric value, Perl will automatically do a lot of things.
The scalar of the number. Perl does not distinguish between integer and floating-point numbers. Perl performs internal storage and numerical calculation based on floating-point numbers. When necessary (for example, it performs an array index $ array [$ Index]), perl performs some internal conversions, but it is invisible to programmers and uncontrollable. In fact, it is not required. The Design of Perl in the numerical quantity also determines that Perl is not suitable for a large number of numerical computing programs (through external means such as modules to accelerate the computation, which is not within the scope I am talking about ), perl itself was born to process text. In this use scenario, we abstract and weaken the number of values and concentrate on strings. I like this kind of focus!
Array and list, I have not figured out the necessity of both, or the difference between them. I read the fourth edition of Perl language, a PDF file with less than 2 MB and more than 200 pages on the Internet. After this book talks about the list, it basically only has the list, and the code of $ XXX [$ I] is missing. I forgot the array for the moment. Remember the list.
Hash and "Dictionary" (Python) or "map" (golang) in other languages are a concept. Larry calls this data structure a hash because its internal implementation uses a hash table. It's a strange name to think of "scalar! Sometimes common things are called common names, which makes it easy to communicate with others. In other cases, hash refers to a technology related to security and encryption, an algorithm, a function, and a special string! It's nice to call a hash table. These Masters sometimes think differently. As Perl users, you must accept hashing.
These four data types are located in different namespaces. Different prefixes (scalar $, list @, and hash %) are used for variable naming. Therefore, the same name can be used, but does not affect each other. Take the following example:
#!/usr/bin/perl$v="hello world";$v[0]="hello";$v[1]="hello world";@v = qw/hello world/;%v = qw/hello world hi planet/;print "\$v=$v\n";print "\$v[0]=$v[0] \$v[1]=$v[1]\n";print "\@v=@v\n";print "\@v[0]=@v[0] \@v[1]=@v[1]\n";print "$_ in \@v\n" for @v;print "$_ => $v{$_} in %v\n" for keys %v;
The output result is:
$v=hello world$v[0]=hello $v[1]=world@v=hello world@v[0]=hello @v[1]=worldhello in @vworld in @vhi => planet in %vhello => world in %v
Perl has a design principle that does not impose additional restrictions on programmers. From the above program, we can see that variable naming is indeed free. The author of Perl language introduction recommends that programmers do not adopt the practices in the above Code, which will cause inconvenience to program maintenance. In fact, I think that because the internal implementation of the four data types is located in different namespaces, there will be no duplicate names. If different types of variables cannot have the same variable name, A special check is required. In some possible implementation mode, this will add additional coding work. Therefore, it may be a type of laziness to do a duplicate name check. In other languages, we have never seen similar syntax rules. This "freedom" in Perl should be considered abolished.
In general, Perl has a lot of data types in common with shell, especially scalar. After all, Perl started to work on UNIX. The Perl data type system has a great impact on PHP. php is further simplified and the variable type is weakened. The variable prefix is $. arrays, lists, and hashing are also merged into arrays in PHP. I think this $ symbol is redundant, as it does not exist before variables in Ruby (Perl also has some influence on it.