Chapter 2 scalar data in Perl

Source: Internet
Author: User
Tags control characters scalar
Preface:

Definition:The concept of singular and plural: Scalar is a single thing. perl uses scalar (the simplest data type in perl) to call a single thing. For most scalar values, it is either a number or a string composed of characters (perl does not have the concept of a single character, and a string is a single thing as a whole ).

Association:The scalar can be associated by using an operator (such as addition or string join), and the result is also a scalar.
Source and Destination:A scalar can be stored in a scalar variable or read (or written to) from a file or device ).

1. Number

The internal formats of all numbers are the same:Both integers and floating-point numbers are based on "double-precision floating-point numbers" Inside perl (that is, the double type of C compiled for perl, most modern systems use the IEEE-745 format) to save and perform operations.
Direct floating point quantity:
Concept of direct quantity: Literal (a literal constant) is a numerical value written in perl code. The direct amount is not the result of an operation, nor the result of an I/O operation, but the data written directly in the source code of the program.
Method of writing directly:General Syntax: 12.45; Scientific Notation:-6.5e24: negative 6.5 multiplied by 10 to the power of 24 (e can also be written as E ).
Integer direct quantity:2001,-59, 23455432, etc.
Note:To make it clear, Perl allows 2345432 to be expressed as 2_345_54_32.
Non-decimal integer direct quantity:
Octal: starts with 0 (zero), for example, 037 ----- corresponding decimal: 225
Hexadecimal: starting with 0x (zero), for example, 0xff ---- decimal: 225
Binary: starting with 0 B (0), 0b11111111 ---- decimal: 225
That is, the above three numbers are completely equivalent, and 0b11111111 can also be written as 0b1111_1111 (much clearer ).

Number OPERATOR:Addition (+), subtraction (-), multiplication (*), Division (/), modulo (%), and multiplication.

2. String

Definition:The strings in perl can beAny characterAny combination of null characters (NUL) has no special significance in perl, because perl will remember the length of the string separately, rather than using NULL characters to indicate the end of the string.
Features:The shortest string is an empty string without any characters; the longest string length is unlimited ------- conform to perl as much as possible"No built-in restrictions" Principle.
Form:
Single quotes:
Strings in single quotes, except single quotes and backslash characters, all characters in single quotes represent themselves.
Double quotation marks:Similar to single quotes, but the string in double quotes has a larger and more complete backslash character, which can be used to indicate control characters or use octal notation (or hexadecimal) represents any character.
Note:Backslash escape: Add different characters through the backslash to represent data in different formats.
--------------------------- Single quotation marks and double quotation marks in Linux --------------------------------------------------------------
In Linux, both single quotes and double quotes can disable shell processing for special characters. The difference is:
Single quotes are more rigorous. They close all characters that have special functions.
The double quotation marks will open three options, specifically: (1) dollar sign: $; (2) reverse quotation marks :~; (3) backslash :\. That is, these three characters are not ignored (with special significance ).
--------------------------------------- End ------------------------------------------------------------------------------
String OPERATOR:".": Connection string;"*": Repeat connection. The number of repeat attempts is;
Automatic conversion between numbers and strings: Perl is automatically converted according to the actual situation. When converting a string to a number: Ignore the leading blank spaces and non-numeric parts of the string (if there is no number, convert it to 0); Number
When converting to a string, convert numbers to strings with the same (output effect ------Perl automatically converts data without the need of programmers..

3. perl built-in warning information

When the debugging program is, to display the program warning information, let perl issue a warning, use the-w option to enable the warning function. For example, perl-w my_program
If you want to enable the warning function for each running, you can use #! Line :#! /Usr/bin/perl-w
Note:In perl 5.6 or later versions, you can also enableCompile commandTo enable the warning function:
#! /Usr/bin/perl
User warnings;
For more details, see perl-M diagnostics or use diagnostics.;------- This compilation command slows down program startup.

4. Variable scalar


Definition
A variable is the name of a container. It can store one or more values.
Perl identifier: A letter or underline. It can be followed by multiple characters, numbers, or underscores.And there is always a $ symbol before the scalar variable..
Select the variable name with caution: Make sure the name is correct.
Scalar Value assignment (=) and binary value assignment operator (+ =,-= ...)

5. Print the output result

Default output to the screen,If multiple values are output, separate them with commas (,)..
Interpolation of scalar variables in a string:You can use either double quotation marks or single quotation marks.
Some experiences: escape characters can be used to output special characters and variables themselves. variables can be enclosed in curly brackets to avoid ambiguity.
Operator priority and tuberculosis:
Comparison operator:

Comparison Value String
Equal = Eq
Not Supported ! = Ne
Less < Lt
Greater > Gt
Less than or equal <= Le
Greater than or equal > = Ge
6. if Control Structure

Boolean value:Perl does not have a dedicated Boolean data type. It is determined by the following rules:
(1)If the value is a number, 0 is false, and other values are true.
(2)If the value is a string, a null string ('') Is false; others are true.
(3)If it is neither a number nor a string, it is converted to a number or string before judgment.
Note:Because the string'0'It is the same scalar value as the number 0. Therefore, perl is treated as false.
Get user input:The simplest way: <STDIN>, for example, $ line = <STDIN> reads the standard input to the scalar value line until a line break (that is, press the Enter key) is encountered ).
Note:The string returned by <STDIN> is generally followed by a linefeed (unless an exception is terminated ).

7. chomp Operator

DeleteEnd of stringLine Break. Essentially a function. The return value is the number of characters actually removed (generally 1 ).

8. undef variables and defined functions:

UNDEF:Before being assigned a value for the first time, the initial value of the variable is a special undef (undef undefined -- "null"): a data type: neither a number nor a string.
However,Undef can be randomly converted as needed: when used as a number, it is converted to zero; when used as a string, it is converted to a null string.
For example, the "line Input" operator <STDIN> sometimes returns undef: when the end of the file is read, undef is returned to indicate this condition.
Defined Function:Determines whether a string or number is undef. If it is undef, false is returned; otherwise, true is returned.


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.