Variable types in Perl

Source: Internet
Author: User
Tags control characters

There are five variables in Perl:

Scalar, pointer, array, hash and handle.

1. scalar

Let's take a look at scalar. scalar is the simplest data type in Perl. Variables of this data type can be numbers, strings, floating-point numbers, and are not strictly distinguished. Add "$" before the variable name during use, which indicates that it is a scalar, which is equivalent to the first s in scalar.

Use the following format for definition:
$ Myfirst = 123; # Number 123
$ Mysecond = "123"; # string 123
$ Mythird = "$ myfirst, $ mysecond"; # a string of 123,123
$ Myforth = '$ myfirst, $ mysecond'; # string $ myfirst, $ mysecond
A numeric scalar can start with 0 and start with 0x. For example:
$ Var1 = 017; #15 in decimal format
$ Var2 = 0xf; #15 in decimal format
You can also use the following format:
$ Var3 = fig; #1234567
Floating point numbers can be defined as follows:
$ Var4 = 3.4;
$ Var5 = 4.3e + 21;
$ Var6 = 234e345;

In Perl, strings are represented by a scalar. The definition method is similar to that in C, but in Perl, strings do not end with 0. In addition, double quotation marks and single quotation marks define strings differently. Variable names in strings defined by double quotation marks are replaced, and control characters such as "\ n \ r \ t" can appear. If you use single quotes, you cannot use the backslash-guided Control Command (of course, you can also use single quotes and backslash). The text will be saved as the original character string.
However, multiple lines of text can be defined using single quotes, as shown below:
$ Var7 = 'This is a usage
Multi-line string text
Example ';
$ Var8 = '\'; # string '\

2. Array

In the Perl language, the array uses the "@" Guide. The meaning of "@" should be equivalent to the first a in the array ). Array initialization uses (). We can use the following method to initialize the array:
@ Var9 = (1, 2, 3, 4, 5 );
When referencing elements in an array, the following format is used:
$ Var10 = $ var9 [1];
The guiding character before the array is changed to "$", which indicates that the element in the array is referenced.
If "@" is used as the boot character, the length of the array will be returned in the preceding example. For example:
$ Length = @ var9; # $ length returns the array length, that is, the number of elements in the array.

Iii. Hash)

A hash table is a very special data structure. I have seen it when studying the data structure, called a hash (SET). Before using the Perl language, I have seen a similar class in BorlandC ++, also called set. Similar data structures are also provided in Javascript, called the connected array associatedarray.

The hash table is guided by "%. The definition method is as follows:
% Var11 = ("hello" => "Haro", "nihao" => "hello ");

The usage is as follows:
Print $ var11 {"hello"}; # output Hello $ var11 {"nihao"} = "hello ";
You can also use the following method to define:
% Var12 = ("hello", "hello", "nihao", "hello ");
The results of this method are the same as those above, but I still like the first method, and we recommend that you use the first method.

4. Handle

A handle is a special type of data used to store pointers to system resources, such as files, directories, pipelines, and sockets. In the latest Perl language, this data is not actually used. The handle type does not contain boot characters. Generally, uppercase letters are used to indicate the handle type. For example, in the following example, open a file, read a row, and close it.
Open (filehandle, "test.htm ");
$ Templine = <filehandle>;
Close (filehandle );
Here <> reads a row from the handle.

5. pointer

Pointers in Perl are classified into two types: Soft pointers and hard pointers. The following describes hard pointers. Use "\" in Perl to generate pointers. Example:
$ Vavr13 = "hello ";
$ Pointerto =\$ var13;
$ Pointerto is a pointer to $ var13. You can use the following method to access the data pointed to by the pointer. Print $ pointerto; that is, add a $. Pointers in Perl are different from those in C. pointers in Perl have reference counts. That is to say, if a data item can be referenced by several pointers, the data will be automatically released when no pointer is referenced.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.