Getting Started with Perl (i) basic types and operators for Perl

Source: Internet
Author: User
Tags bitwise chop comparison numeric logical operators perl operator

Before studying the basics of Perl, we would like to have a chance to see the following Perl introduction. Baidu Encyclopedia

First, basic types of Perl

The basic type of per is divided into two types: numeric type and string type.

Numeric types can be subdivided into integer types. such as 123. Floating point type. such as 123.123, 0.123456. Scientific counting type. such as 1.23E2, 1.23e2. A large numeric type. such as 1_156_123_585_123.

String string. such as "123456", ' 123 ', QQ (this is a string too!)

Second, use the variable to store the base type value

Perl defines variables very simply. The format is: $ variable name (Ps:perl programmer must want Money to go crazy); The reference is also simple: the $ variable name.

A simple example is given below:

#!user/bin/env perl-w
$int = 123456;
$float = 123.456789;
$sci _notation1 = 1.2E10;
$sci _notation2 = 1.2e10;
$big _num = 1_789_854_654_894_654_355_223;

$string = "This is a string";

printf "int value: $int \ n";
printf "float value: $float \ n";
printf "Sci_notation1 Value: $sci _notation1\n";
printf "Sci_notation2 Value: $sci _notation2\n";
printf "Big_num Value: $big _num\n";
printf "string value: $string \ n";

The output is as follows

int value:123456
float value:123.456789
sci_notation1 value:12000000000
sci_notation2 value : 12000000000
big_num value:1.78985465489465e+021
string Value:this is a string

Simple explanation: The Science and technology type value in the e-case-insensitive large numeric type is separated by "_" for readability only, using the method of thousand, you can also use the extreme method, you can also directly do not use "_" Print and printf are built-in output functions. If you want to receive keyboard input data you can use the following format: $from _key_board = <STDIN>; Receive input from DOS window, store in $from_key_board

Note: If the data is too long, there will be rounding on the precision. The representation of a character type can use double quotation marks "", or single quotation marks, which must appear in pairs. The backslash "\" can be escaped when the inner content of the single quotation mark contains a single quotation mark or double quotation marks inside the inner content. such as: "Tom said:\" It is rainning outside!\ "". Perl provides an alternative string representation of QQ for double quotation marks (not Tencent QQ), Q for single quotation marks, and for use with the boundary character. The bounds can make any non-whitespace, tab, and newline characters. For example: QQ (Tom said: "It is raining Outside"). However, it is not recommended to use numbers and letters as the boundary character, which can cause the program to read difficult. But it also comes down to the 2nd question, if the internal character creation contains the bounds, 1. The paired appearance is allowed, but the readability is reduced. 2. Use the escape character. 3. There are so many boundary characters, change one. The above printf output has references to the previously defined variables. This method can only be used within the "" or PP, other methods are not available.

Third, Perl operator

Most are like other languages, if you have a certain language basis, you can only look at the Scarlet Letter Section

Assignment operator = Assignment = = For example: $num + = 1; Equivalent to $num = $num +1; -= For example: $num-= 1; Equivalent to $num = $num-1; *= For example: $num *= 1; Equivalent to $num = $num * *; /= For example: $num/= 1; Equivalent to $num = $num/1; %= For example: $num%= 1;         Equivalent to $num = $num% 1; #取模, divide by that number to take the remainder **= for example: $num **= 2;          Equivalent to $num = $num **2; The #** represents a power, $num 2 times. = For example: $str. = "string" equivalent to $str = $str. "String"; Represents a connector, stitching together two strings

Arithmetic operator +-*/% take remainder * * Power exponent 10**2 that is 10 of 2 + +--. The connection string, "This is a". " string "=" This is a string "

Numeric relational operator > greater than >= greater than or equal to < less than <= less than equals = = equals = = Not equal to <=> greater than: 1; equals: 0; less than:-1;

The string relational operator GT is greater than GE greater than and equal to LT is less than or equal to EQ equals ne not equal to CMP greater than: 1; equals: 0; less than:-1;

logical operators && OR and full true is True | | Or OR or all false. Or not false true XOR different or same false true

Bitwise operations & Bitwise-and | Bitwise OR ~ bitwise NON ^ bitwise XOR << Left shift >> right shift

Special operators: The interval representation is equivalent to 1..3. : Three mesh operator, satisfies. Pre-condition execution: Previous content, not satisfied with execution: Post content

File data operator-R $file if $file is readable, returns a value of 1-w $file if $file is writable, returns the value of 1-x $file if $file is executable, returns the value of 1-e $file if $file exists, returns the value of 1 -O $file if $file is owned by execution, returns the value of 1-S $file returns the file size of $file (bytes)-F $file If $file is a normal file, the value of 1-t $file if $file is a text file, the value of 1 is returned -B $file If $file is a binary file, returns the value of 1-m $file returns the number of days that the $file file was last changed to now

Special attention:

1.Perl does not deliberately emphasize the initialization of variables, if a variable is not initialized to perform the operation, it will take its default value: The number is 0, the string is "" empty string.

Strings in 2.Perl can be calculated with numeric values, and if the string does not look like a number, it will be replaced by 0.

3. Be aware of the difference between numeric relational operators and string-type relational operators. A numeric comparison is used when comparing strings with string relational operators.

If a numeric relational operator is used in a string comparison, such as: $name = "Jack"; $name = = "Tom"; the result of the operation is always 1, for the reason see 2.

(then you take tens of millions of of the data from the database. Don't blame me for not saying it, waiting to be killed by the project manager)

4. In the shift operation, the << is related to the number of operating system bits, such as 1<<64 = 1 in 64-bit operating system, and 64 bits per move is a loop.

5.Perl is considered 0, an empty string, an undefined value of undef is false, and the others are true.

6. String relational operations, a bitwise comparison of strings on both sides from left to right, compared to ASCII table order

7.&& and and are not exactly the same,&& are executed earlier than and

8. The logical operator does not return 0 or 1, but instead returns the result of the processing condition before the end of the decision. If 1&&2 returns 2, 0&&2 returns 0,1| | 2 returns 1.

Perl Common functions

printf: output function, using the same as in C language;

Length: The output contains the lengths of content: such as length ("asdf") = 4;

Chop: Remove the last character: Chop ("abcd") = "abc";

Chomp: Remove the trailing carriage return line: Chomp ("abcd\n") = "ABCD";

substr ($string, m,n): Given a string, start intercepting n bit lengths from the ground m bit: substr ("Hello", 2,3) = "Llo", counting starting from 0, n can be omitted, i.e. to the tail

 

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.