Perl language learning notes Day 1

Source: Internet
Author: User
Tags scalar

1. Perl

Practical Extraction and report language: Practical excerpt and Report Language

2. Multi-line comment

(1) = requires top level writing

= Description

Statement;

= Cut

(2) The description at the end must be written in the top level.

<Description;
Print "Hello, world! \ N ";
Description

3. Number:

All numbers are stored in Perl in double-precision floating-point format;

5 ** 3: 5*5*5;

4. String:

The shortest string is a null string, and there is no limit on the longest string. (Perl stores the length of strings)

Single quotation mark string: only single quotation marks and backslash can be escaped. For example, "\ n" indicates the backslash and N, not line feed.

Double quotation mark string: supports variable interpolation and various backslash escaping. For example, "\ n" indicates a line break.

Backslash escape:

\ L: Convert the next character to lowercase; \ L: Convert all characters until \ e to lowercase;

\ U: converts the next character to uppercase; \ U: converts all characters until \ e into uppercase;

\ Q: Add a backslash to non-word characters until \ e;

\ E: End \ L, \ U, \ q

Use periods (.) to connect strings, such as "hello". "world" = "helloworld"

String repetition operator: X. The number on the right (taking an integer) indicates the number of repetitions. For example: "Hello" x 3 = "hellohellohello", 5x4 = "5555"

Variable interpolation:

$ Word = "hello ";

$ Words = "$ word world! "=" Hello world! ";

If $ word is followed by a character, you can use: $ words = "$ {word} world! ";, Or $ words =" $ word "." World! ";

5. Perl automatically converts strings and numbers as needed.

In mathematical computation, the non-numeric part of the string will be skipped, for example, "12read32" * 3 = 36;

Convert strings without digits to 0;

6. Warning Information

(1) Run: Perl-W my_program

(2) declare in the program :#! /Usr/bin/perl-W or #! Perl-W

(3 )#! /Usr/bin/perl

Use warnings

(4) More detailed, will lead to slow program startup:

#! /Usr/bin/perl

Use diagnostics

7. Variables

It starts with $ and is case sensitive. The variable is generally in lower case.

8. Comparison Operators

==: EQ ,! =: NE, <: Lt,>: GT, <=: le, >=: Ge;

9. Boolean Value

Number: 0 is false. All other values are true.

String: The Null String is false, and all others are true.

Other types: convert to a string or number for further judgment

The string '0' and digit 0 are a value, so they are false.

10. obtain user input: <stdin>

Chomp: removes the last line break at the end of the string and returns the number of removed characters (0/1). For example, Chomp ($ text = <stdin>): reads the input, excluding line breaks;

You can remove the line breaks of the elements in the list at one time, for example, Chomp (@ lines)

The data entered in the command line carries a line break. By default, chmop is used for processing.

11. UNDEF: The number is 0, and the string is an empty string.

Defined ($ N): determines whether Variable N is defined.

$ N = UNDEF: creates an undefined variable;

12. Array

Subscript of the last element in the array: $ # arry and-1

@: Reference the entire array. For example, @ rocks = QW/bedrock slate lava/= {"Bedrock", "slate", "lava "}

The array name is expanded into an element list and assigned to the new array one by one. For example, @ quarry ={@ rocks, "new", UNDEF }={ "Bedrock", "slate ", "lava", "new", UNDEF}

Pop and push are used to operate on the last element of the array: $ num = POP (@ arry) = pop @ arry; or pop @ Arry. If the array is empty, pop returns UNDEF, you can push an array to another array.

Shift and unshift are used to operate the first element of the array, $ num = shift (@ arry), unshift @ Arry, @ others

The array content can be inserted into double quotation marks and separated by spaces. For example, print "quartz @ rocks lime"> "quartz bedrock slate lava". This will lead to incorrect email addresses, you can use \ escape or single quotes.

A variable is followed by "[" and must be separated by "[" to avoid being treated as an array. For example, print "this is $ (fread) [3] ", print" this is $ fread \ [3] ";

13. Range OPERATOR:... example: (1... 5) = (1, 2, 3, 4, 5)

14. QW Abbreviation: QW {Apple good hello }={ "apple", "good", "hello "}

You can use any punctuation mark as the separator: QW {Apple good hello} = QW (Apple good hello) = QW! Apple good hello! = {"Apple", "good", "hello "}

15. list assignment

($ First, $ last) = ("first name", "last ");

The number of variables on the left is smaller than the number of values in the list, and extra values are ignored. The number of variables on the left is greater than the number of values in the list. The unassigned variable is UNDEF.

16. foreach can traverse the values in the list. After traversing, the control variable is still the value before the loop. For example, foreach $ rock (@ rocks) (); after the loop, $ rock is the original value.

17. $ _

The default variable of Perl. When no variable or value is specified, the value of $ _ is used. For example, when foreach does not specify the traversal value, use "$ _" instead.

18. Reverse

Returns list elements in reverse order.

19. Sort

Reads the value of the list, sorted by internal encoding, generally ASC | encoding.

Numbers cannot be sorted as strings. Otherwise, 101 will be placed at the top 90 side, and the number starting with 1 will be placed at the top 9 side.

20. Context

List context, generating a list of elements

Number of elements in an array generated in a scalar context

Forcibly specify the scalar context: Scalar

21. Clear the array: @ arry = {};

Incorrect practice: @ arry = UNDEF; get a list with only one undefined element.


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.