Home  >  Article  >  php教程  >  Perl variables (1)--pure variables

Perl variables (1)--pure variables

黄舟
黄舟Original
2016-12-16 13:56:311149browse

Perl has three types of variables:

Pure variable (ScalarVarible)
Array (Array)
Associative array (Associative array)
1. Pure variable
Also known as scalar variable, it is the simplest data type processed by Perl. Scalars can be numbers (such as 2, 3 or 2.5e6) or strings (such as "hello" and "online school").

Scalar variables in Perl start with the dollar sign $ and a letter. Later, they can be letters, numbers, and underscores. There is a distinction between upper and lower case, and all letters, numbers, and underscores are valid. For example:

$a and $A are different variables;
$this_is_a_long_variable_1 and
$this_is_a_long_variable_2 are different variables;

Variables in Perl can generate new variables through operators (such as or., etc.). You can read variables from files and devices, as well as output them.

When using scalar variables, you must add the $ sign in front. Note: If the specified pure variable is a character, you must add "" double quotes or single quotes; if it is a numerical value, you do not need to add "". symbol.

Scalar data can be divided into numbers and strings:

Numbers
can be divided into integer variables and floating-point variables.
Integer variables: (such as 2, -200, 3568, etc.). Perl supports octal and hexadecimal variables. Octal starts with 0 (such as 0255, which represents 255 in octal), and hexadecimal starts with 0x or 0X (such as -0x1a, which represents negative 1A)
Real type Variable: (such as 2.5,-6.3e5,-2.3-e6, etc.).

String
The shortest string can have no characters, and the longest can fill up your memory. This is consistent with Perl's "no built-in limit" principle.
Strings have two formats: single-quoted strings and double-quoted strings.
Single-quoted string: It is a string of characters enclosed in single quotes. The single-quoted string is not part of the string. Any character can be inserted within quotation marks. There are only two exceptions, one is to insert a single quotation mark in the middle and have a backslash in front; the other is that the string has two consecutive backslashes.
Double-quoted string: It is a string of characters enclosed in double quotes, its function is similar to C language.

Backslash escape table in double-quoted strings
Structural meaning
n Line feed
r Carriage return
t Horizontal tab character
f Form feed character
b Backspace
v Vertical tab character
a Bell
e Esc

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn