Chapter 2: subprocesses/functions
- Declaration: subfunction_name;
- Definition: subfunction_name {...}
- Function call is the same as C:
-
- Example_subroutine (); or example_subroutine;
- Example_subroutine ('perl is ', 'my favorite', $ language); or example_subroutine 'perl is ', 'my favorite', $ language;
- Make sure that the previous declaration/definition is available during the call.
- Return Value: the last line of code of the function can be written into an expression as the return value, for example, $ total. You can also use the return statement to return the return value anywhere in the function body.
- The function accesses the input parameters through the @ _ variable:
- PASS Parameters by reference in Perl. Changing input parameters will affect variables outside the function.
- Tips for implementing default parameter values:
Sub log_warning { My $ message = Shift | "something's wrong "; My $ time = Shift | localtime; # default to now. Print "[$ time] $ message \ n "; } |
- Tips for parameter naming:
- Call: Logon (username => $ name, password => $ pass, host => $ hostname );
- Definition:
Sub logon { Die "parameters to logon shocould be even" If @ _ % 2; My % ARGs = @_; Print "logging on to host $ ARGs {hostname} \ n "; ... } |
Chapter 2: Regular Expressions
- The regular expression is enclosed in a slash (/). You can use a variable in the slash (/) to replace the variable with the corresponding value.
- Regular Expression match: Str = ~ /RE/If STR matches/RE/, the success value is true, and the reverse value is false ;!~ And = ~ Opposite
- /RE/is equivalent to $ _ = ~ /RE/
- After matching, you can use $1, $2, $3... to obtain the matched substring in the string.
- Because I do not know how many substrings have been matched successfully, I need to use the defined operator to check whether $1, $2, $3 have been defined, stupid
- // Use \ escape :.*? + [() {^ $ | \/
- \ Q can be used to close metacharacters and \ e to end \ Q. \ Q... \ e is free to use the metacharacters mentioned in the previous key.
- Anchor ^ indicates matching from the start of the string, while $ indicates matching to the end of the string.
- [] A character set that can be defined. For example, [ABC] matches A, B, or C, [0-9] matches 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9. In [], ^ is used to express the antsense, that is, [^ EO] can match any character except E and O.
- Several equivalence values: \ D is equivalent to [0-9], \ W is equivalent to [0-9a-za-z _], \ s is equivalent to [\ t \ n \ r \ f], \ D is equivalent to [^ 0-9], \ W is equivalent to [^ 0-9a-za-z _], \ s is equivalent to [^ \ t \ n \ r \ f],. equivalent to any character (except for line breaks)
- | Can be used to represent or, for example, yes | maybe can match yes or maybe, Ye (S | T) to match yes or yet
- ? Meaning? The previous mark appears 0 or 1 time, for example (Word )? Is can match word is and is
- + The value is 1 or more times, and * is any time (including 0 times)
- /Bea? T/match beat and bet
- /BEA + T/match beat, beaat, beaaat...
- /BEA * t/match bet, beat, beaat, beaaat...
- {} Defines the number of occurrences. For example, \ s {2, 3} can match two spaces or three spaces. {X} indicates X and {X,} indicates X and above.
- Working principles of Regular Expression matters:
- Once the match fails (the next character cannot meet the regular expression definition), the match will be stopped immediately.
- Once the matching is successful, the matching will be ended immediately.
- + *? Take the longest match for the three characters, that is, match the Regular Expression and string after the match fails.
- Recent branch principle: if there is a branch match (for example, [A-Z] Or Ye (S | T), try from the beginning to the back. If the attempt fails, take the next one, until the matching is successful or all cases fail.
- Replace with regular expression: Str = ~ S/find/replace/; can be used to replace the first substring that meets the find mode in STR with replace. If STR is not specified, execute S/find/replace /; for $ _ Operations, add a G modifier at the end to replace all substrings
$ _ = "Awake! Awake! Fear, fire, foes! Awake! Fire, foes! Awake! "; S/foes/flee /; Print $ _, "\ n "; Output: Awake! Awake! Fear, fire, flee! Awake! Fire, foes! Awake! |
$ _ = "There are two major products that come out of Berkeley: LSD and Unix "; S/(\ W +) \ s + (\ W +)/$2 $1/g; Print $ _,"? \ N "; Output: Are there major two that products out come Berkeley of: And lsd unix? |
- Similar to Q // and QQ //, // and S // you can also use other separators. When other separators are used in matching, use m as the prefix, for example M/^ \ s * [A-Z]/; M # ^ \ s * [A-Z] #; m {^ \ s * [A-Z]}; s/old text/new text/g; can be written to s {old text} {New text} g; can also be written:
S {old text}
{New text} g;
Another example is S/\/usr \/Local \/share \ // \/usr \/share \/g; it can be written to s #/usr/local/share/#/usr/share/# G;
- Modifier
- /M treats the string as a multi-line string. In this case, ^ $ does not match the first and end of a word, but matches the first and end of a line.
- /S treats the string as a single line string. In this case, all characters including linefeeds are matched.
- /G: If Multiple matching substrings exist, all are matched. If yes, all matching substrings are replaced, when using this modifier, you can use \ G anchor to match the last character position of the last matching substring.
- /X allows you to write regular expressions in multiple rows and add comments.
- Split (Re, STR) divides STR into multiple substrings according to RE and returns a list containing these substrings. If STR is not specified, the $ _ operation is performed by default. For example, Split/:/, "Kake: X: 10018: 10020:/home/Kake:/bin/bash" ("Kake", "X", "10018 ", "10020", "", "/home/Kake", "/bin/bash ")
- Join (STR, list) concatenates each element in the list into a string and returns this string, for example, join "#", ("Kake", "X ", for "10018", "10020", "", "/home/Kake", and "/bin/bash", "Kake: X: 10018: 10020 :: /home/Kake:/bin/bash"
Chapter 2: documents and data
- Open a file: open (filehandle, mode, filename) such as open (FH, '<', 'input.txt ')
- Returns true if it is enabled successfully, and false if not.
- You can also write open (FH, '<input.txt ')
- Access Mode:> write <read> append
- Close (filehandle) is used to close the file handle, such as close (FH );
- Scalar read: My $ line = <FH>, read a row
- Read list: My @ list = <FH>. Read all rows. Each row contains a list element.
- Special file handle: <> (called "diamond operator ")
- <Stdin>
- Otherwise, it is the specified file handle. You can specify multiple files, which can be opened one by one and read row by row in the specified order.
- @ Argv is a string array that stores command line parameters. For example, when running Perl argv1.pl King Crimson rocks, @ argv is ('King', 'crimson', 'rocks ')
- <> In essence, it is implemented using @ argv. $ Argv: <> the file name pointed to by the current file handle.
- Print FH list can be output to the files pointed to by FH
- $ | Specifies whether to use the OS output buffer. The default value is 0 (used). If it is set to 1, it is not used.
- Pipe: connect the stdout output of the previous program to open (FH, '-|', command); for example, open (FH, '-|', 'perl sort2.pl gettyw.g.txt ');
- Pipe: connect the stdout output of the current program to the stdin: open (sort, '|-', command) of the next program. For example, open (sort, '| -', 'perl sort2.pl ');
- Pipeline: You can use IPC: open2 to create a bidirectional pipeline (not detailed)
- LC (STR) converts the letters in the STR string to lowercase and then outputs the string. UC (STR) converts the string to uppercase.
- File tests similar to shell: (-operator $ file) Operator includes:
- E $ file is true if it exists.
- F $ file is true when it is a file (not a directory ).
- D $ true when file is a directory
- True when Z $ file is 0
- True when S $ file is not 0
- R $ file: true if it is readable
- W $ file can be written to true
- X $ file can be executed as true
- O $ file: true if the current user owns the file
Chapter 2: string processing
- Length (STR) returns the length of Str.
- Index (STR, substr) returns the subscript of the first occurrence of the substr in Str. If the substr does not exist,-1 is returned.
- Index (STR, substr, mindex) specifies to start searching from mindex
- Rindex (STR, substr) returns the subscript of the last occurrence of the substr in STR, and returns-1 if no substr exists.
- Rindex (STR, substr, mindex) specifies to start searching from mindex
- Substr (string, starting_index, length) returns the substring of the length from starting_index.
- Tr // For character replacement. For example, TR/old/new/will create the following character replacement rules: O-> N, L-> E, D-> W, for example, $ string = '000000' and $ string = ~ TR/0123456789/abcdefghij/; or $ string = ~ TR/0-9/A-J/;, $ string becomes 'cabbage'
- D modifier is used to delete characters, such as my $ vowels = $ string = ~ TR/aeiou //; the vowels in $ string cannot be deleted, while my $ vowels = $ string = ~ TR/aeiou/d. For example, $ string = ~ Tr // D; all spaces in $ string can be deleted.
Chapter 2: OS Interaction
- % Env saves all environment variables (changing % env does not really change the system environment variable settings), such as $ ENV {home}, $ ENV {path}, $ ENV {user}, etc.
- File wildcard: the glob (pattern) function is used to return the next file name that meets the pattern, for example, glob ('*. PL '), equivalent to <*. pl>, indicating all files with the PL extension.
While (glob ('*. dat ')){ Print "found a data file: $ _ \ n "; } |
- You can use opendir to open a directory and read the files contained in it through the readdir function. closedir is disabled:
Opendir DH, "." Or die "couldn't open the current directory: $! "; While ($ _ = readdir (DH )){... } Closedir DH; |
- The following functions are the same or similar to Unix APIs. For details, refer to apue (Advanced Programming in UNIX environment).
- Chdir (directory)
- Unlink (list_of_files) supports unlink multiple files
- Rename (old_file_name, new_file_name)
- Link (file_to_link_to, link_name)
- Symlink (file_to_link_to, sym_link_name)
- Readlink (sym_link_name)
- Mkdir (directory_name, Mode)
- Rmdir (directory_name)
- Chmod (file_or_directory_name, Mode)
- System (command)
- The system () function prints the output of the executed command to the console (stdout) and returns the execution result of the process (Personal inference: Return Value of the main function ). You can use 'command' to capture the output, for example:
- $ Output = '$ command'; Save the output of $ command execution to the string $ output, including line breaks.
- @ Output = '$ command'; Save the output of $ command execution to the list @ output. Each line contains one element, including a line break.