# Perl concise tutorial

Source: Internet
Author: User
Tags comments function definition hash regular expression scalar

# Perl concise tutorial
#1. perl uses # as a single line comment
# Multi-line comments are enclosed by pod = cut as follows: multi-line comments

= Pod
Multiline comment
= Cut

#1. Variables
# There are only three variables in perl
#1. Common variables (scalar)
#2. Array variables (list)
#3. hash variable (hash table)
# Declaration method
# Scalar
$ A = 1;
$ Scalar = 10;
$ S = "sdsd ";
$ S1 = 'asdasd ';

# Array
Print @ arr = (1, 2, 3, 4, 5, 6 );
& P;
Print @ arr100 = (1 .. 100 );
& P;
# Hash table
Print $ hash = {'a' => "abc", 'B' => "B "};
Print $ hash-> {};
& P;

# Function definition
# Sub funcname {
# The parameter is in @ _.
# @ _ [0] the first parameter
# @ _ [1] The second parameter
# @ _ [2] The third parameter
# And so on
#}
# Function call
# Add a name before the function name &
Sub p {# This function is used to assist printing
& Println;
}
Sub println {
Print "n ";
}
# Regular expression usage (simple exception)
# The regular expression has only five elements: 1. Metadata. 2. Quantifiers. 3. Group. 4. Reverse reference. 5. Literal
# Search
$ Str = 'abc ';
$ Str = ~ /AB. */; # Use. Metadata and * quantifiers
# Match results in $ &
Print $ &;
& P;
$ Str = "abc1234 ";
$ Str = ~ /Abc (d +)/; # metadata d, quantifiers + grouping (d +)
Print $ &; # match all
& P;
Print $1; # First group
& P;
$ Str = 'abcabc1234 ';
$ Str = ~ /(Abc) 1 (d +)/; # literal abc, Group 1 (abc), reverse reference first group 1, metadata d, quantifiers + Group 2 (d +)
Print $ &;
& P;
Print $1;
& P;
Print $2;
# Replacement
& P;
$ Str = 'abcfireabctoadabc ';
$ Str = ~ S/abc // g; # Replace all abc with an empty g switch to replace all without g, and replace only one
Print $ str;

 

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.