Perl script Learning Guide-notes

Source: Internet
Author: User
Tags perl script

I recently worked as an intern and watched my company write some scripts in Perl. I heard that Perl is still very powerful in text processing. I scanned this book on weekends ~ Recorded ~


UNDEF has no Defined Characters by default, and Perl does not contain null. defined ($ X) function: If the parameter is UNDEF, false is returned.

QW (cs phy geo) is similar to ("CS", "phy", "Geo"). A list can be assigned to an array @ array.

Array OPERATOR: Pop, end of push operation, start with shift and unshift operation, reverse, @ array = sort @ array;

Foreach @ Array {print $ _;} prints an array; $ _ is a very powerful default variable.

The each operation returns a pair of @ array (index, value), which is used in the while loop.

Perl automatically adapts to scalar and list context.

Sub defines a subroutine (function). The parameter is included in the @ _ array, and the calling program is available & Symbol. If it does not conflict with the default function, this parameter can be defined in the function.

Persistent private variable of the function, which is identified by state. This variable remains unchanged each time a function is called.

My limits the range of variables to the current function.


<Stdin> reads input files by row. By default, <> all files in the parameter list are read. For each row ending with \ n, Chomp can be used to remove \ n.

Printf can format the output, and sprintf is more powerful.

File handle: Open config, '>>: utf8', 'filename' or die "failed: $! ", Die will throw an exception, generate the output, close config;

Use the file handle: similar to while (<config>) to read the file. Change the default output file. select Config; print "123" to the config file to replace the default stderr.

Hash array: array of keys and values. values can be assigned to the array to expand the hash. The reverse is changed to value => key: % EVN environment variable.

Keys % hash returns the keys array, and values % hash returns the values array; each returns K, V; exists, delete Functions


Powerful Regular Expression: \ A indicates the beginning; \ Z indicates the end;/s matches any blank space; brackets can capture the variable $1 and survive until the next match is successful;

No capture mode :/(? : Bronto )? AAAA (BB | CC)/$1 indicates BB or CC at this time ;/(? <Name1> \ W +)/name capture, $ + {name1 };

Automatic Variable Capture: $ 'variable after matching $ & matched $';/xxxx/p enable variable Capture: High Version $ {^ match}

S // replace, similar to SED, supports capturing $1; \ U conversion to uppercase

Split/regular/, $ string: Split the string into an array; join function, reverse split process;

Non-Greedy quantifiers: + ?, *?, {8 ,}?, No greedy match will be performed;/I ignore case sensitivity and/M match line breaks

$ ^ I = ". Bak" can update the file. When opening the file, open it with filename. Bak and write it back to the original file.


Unless (CON): similar to If (! Con); until: the condition is false and the execution is repeated; last is similar to break; next is similar to continue; Redo is returned to the beginning of the loop body (new); labels can be added.

Various file test characters-E and-M test file information; stat $ filename returns a series of information about the file; my @ files = glob "*": All file names in the current path

String function: Index, substr; advanced sorting @ res = sort {$ A <==>$ B & $ a cmp $ B} @ Array

Smart match character :~~ : Match the hash key, whether the array is the same




Given-When statement: similar to switch, with continue and break;


Add a command to the system to execute sub-processes. Execute exec to another process, and Perl will not execute any more. Back quotes can capture the returned values;

Supports fork multi-process; defined (my $ pid = fork) or die "$! "; Unless ($ PID) {sub-process work}; Support for sending and receiving SIG;

Slice: ($ first, $ last) = (sort @ array) [0,-1]. The array and hash values are acceptable. If an eval capture error is returned;

Grep: My @ array = grep {$ _ % 2} 1 .. 1000; odd array; my @ array = map {& big_money ($ _)} @ data; each element in operation data; returns a list






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.