I. Process processing functions
1. Process startup Function
Function Name eval
Call the syntax eval (string)
The description regards string as a Perl statement execution.
After the command is correctly executed, the system variable $ @ is an empty string. If an error occurs, $ @ is the error message.
Example $ print = "print (\" hello, world \ n \");";
Eval ($ print );
Output result hello, world
Function Name: system
Call the syntax system (list)
The first element in the list is the program name, and the rest are parameters.
System starts a process to run the program and waits for it to end. After the program ends, the error code shifts left eight bits to return values.
Example @ proglist = ("echo", "hello, world! ");
System (@ proglist );
Output result hello, world!
Function Name fork
Call the syntax procid = fork ();
Describe the creation of two copies of the program-parent process and child process-run simultaneously. The child process returns zero, and the parent process returns non-zero.
Value, which is the ID of the sub-program process.
Example $ retval = fork ();
If ($ retval = 0 ){
# This is the child process
Exit; # this terminates the child process
} Else {
# This is the parent process
}
No result output
Function name pipe
Call the syntax pipe (infile, outfile );
This topic explains how to use fork to communicate with Parent and Child processes. The information sent to the outfile file variable can be
Read from infile variables. Steps:
1. Call pipe
2. Use fork to divide the program into Parent and Child Processes
3. One process disables infile and the other disables outfile.
Example: pipe (INPUT, OUTPUT );
$ Retval = fork ();
If ($ retval! = 0 ){
# This is the parent process
Close (INPUT );
Print ("Enter a line of input: \ n ");
$ Line = <STDIN>;
Print OUTPUT ($ line );
} Else {
# This is the child process
Close (OUTPUT );
$ Line = <INPUT>;
Print ($ line );
Exit (0 );
}
Result output $
Program
Enter a line of input:
Here is a test line
Here is a test line
$
Function Name exec
Call the exec (list) syntax );
Similar to system, the difference is that the current program is terminated before the new process is started. Often used with fork, when fork is divided into two
After the process, the child process starts another program with exec.
Example
Result output
Function Name: syscall
Call the syntax syscall (list );
Describe how to call a system function. The first element of the list is the system call name, and the rest are parameters.
If the parameter is a number, it is converted to the integer (type int) of C ). Otherwise, the pointer of the string is passed. For more information, see the UNIX help Perl document.
Syscall must contain the syscall. pl file, that is:
Require ("syscall. ph ");
Example
Result output
2. Process Termination Function
Function Name: die
Call the syntax die (message );
Describe terminate the program and output an error message to STDERR. Message can be a string or a list. If the last parameter
If the number does not contain line breaks, the program file name and row number are also output.
Example die ("Cannot open input file ");
Cannot open input file at myprog line 6.
Function Name warn
Call the syntax warn (message );
The commentary is similar to die. The difference is that the program is not terminated.
Example warn ("Danger! Danger! \ N ");
Result output Danger! Danger!
Function Name exit
Call the syntax exit (retcode );
Describe terminate the program and specify the return value.
Example: exit (2 );
No result output
Function Name kill
Call the syntax kill (signal, proclist );
Sends signals to a group of processes.
Signal is the digital signal sent, and 9 is used to kill the process.
Proclist is the list of process IDs. For more information, see kill's UNIX help.
Example
Result output
3. Process Control Functions
Function Name sleep
Call the syntax sleep (time );
Pause the program for a period of time. Time is the number of seconds to stop. The returned value is the actual number of seconds to stop.
Example: sleep (5 );
No result output
Function Name: wait
Call the syntax procid = wait ();
Pause the program execution and wait for the sub-process to terminate.
No parameter is required. The return value is the sub-process ID. If no sub-process exists,-1 is returned.
Example
Result output
Function Name: waitpid
Call the syntax waitpid (procid, waitflag );
Pause the program execution and wait for the specified sub-process to terminate. Procid is the ID of the waiting process
Example $ procid = fork ();
If ($ procid = 0 ){
# This is the child process
Print ("this line is printed first \ n ");
Exit (0 );
} Else {
# This is the parent process
Waitpid ($ procid, 0 );
Print ("this line is printed last \ n ");
}
Result output $ program
This line is printed first
This line is printed last
$
4. Other control functions
Function Name caller
Call syntax subinfo = caller ();
Returns the caller's program name and row number for Perl Debugger.
List of three elements returned:
1. Package name at the call
2. Caller's file name
3. the row number at the call
Example
Result output
Function Name chroot
Call the syntax chroot (dir );
For details about how to change the root directory of a program, see the chroot help.
Example
Result output
Function name local
Call the syntax local ($ variable );
This topic defines local variables in statement blocks (a set of statements surrounded by braces ).
The change does not affect the variable with the same name outside the block.
Never use it in a loop. Otherwise, a new local variable is defined for each loop!
Example
Result output
Function name times
Call syntax timelist = times
Returns the time consumed by the program and all sub-processes.
List of four floating point numbers returned:
1. User time consumed by the program
2. System Time consumed by the program
3. User time consumed by sub-Processes
4. System Time consumed by sub-Processes
Example
Result output
Ii. mathematical functions
Function Name sin
Call the syntax retval = sin (value );
The comment parameter is a radian value.
Function name cos
Call the syntax retval = cos (value );
The comment parameter is a radian value.
Function Name atan2
Call the syntax retval = atan2 (value1, value2 );
Describe the operation and return the arctan value of value1 divided by value2. Unit: radian, range:-PI ~ PI.
Example:
Converts an angle to a radian subprogram. Sub degrees_to_radians {
Local ($ degrees) = @_;
Local ($ radians); 11:
$ Radians = atan2 (1, 1) * $ degrees/45;
}
Function Name sqrt
Call the syntax retval = sqrt (value );
Describe the square root function. Value is not negative.
Function Name exp
Call the syntax retval = exp (value );
Returns the value power of e.
Function Name log
Call the syntax retval = log (value );
Describe the natural logarithm Based on e.
Function Name abs
Call syntax retval = abs (value );
Describe the absolute value function. (Not in Perl 4)
Function Name rand
Call the syntax retval = rand (num );
Returns a floating point number between 0 and an integer num.
Function Name srand
Call the syntax srand (value );
Describe the random number generator initialization. Make sure that rand is randomly called each time.
Iii. string processing functions
Function name index
Call the syntax position = index (string, substring, position );
Returns the position of the substring in the string. If not, returns-1. Parameter position
Is optional, indicating the number of characters that have been skipped before the match, or starting from this position.
Function Name rindex
Call the syntax position = rindex (string, substring, position );
The description is similar to the index, with the difference being matching from the right side.
Function Name length
Call the syntax num = length (string );
Returns the length of a string, or the number of characters.
Function Name pos
Call syntax offset = pos (string );
Returns the position of the last pattern match.
Function Name substr
Call the syntax substr (expr, skipchars, length)
Describe the substring In the extracted string (or the string generated by the expression) expr, skip the skipchars character, or
This parameter indicates that the child string is extracted from the position skipchars (the first character is 0) and the length of the Child string is length.
Ignore, which means to take all the remaining characters.
When this function appears on the left side of the equation, expr must be a variable or an array element. In this case, the molecular string in the middle is on the right side of the equation.
.
Function Name study
Call the syntax study (scalar );
An internal format is used to increase the access speed of a variable. At the same time, it only takes effect for one variable.
Function Name lc
Uc
Call the syntax retval = lc (string );
Retval = uc (string );
Converts all strings to uppercase or lowercase letters.
Function Name lcfirst
Ucfirst
Call the syntax retval = lcfirst (string );
Retval = ucfirst (string );
The description converts the first letter into uppercase or lowercase letters.
Function Name quotameta
Call the syntax newstring = quotemeta (oldstring );
Add a backslash (\) to the front of a non-word letter (\).
Statement: $ string = quotemeta ($ string );
Equivalent to: $ string = ~ S/(\ W)/\ $1/g;
It is often used in pattern matching to ensure that no character in the string is considered as a matching operator.
Function Name join
Call the syntax join (joinstr, list );
Comments: Combine the string list (array) into a long string and insert the string joinstr between each two list elements.
Function Name sprintf
Call the syntax sprintf (string, fields );
Similar to printf, the difference is that the result is not output to the file, but is assigned to the variable as the return value.
Example $ num = 26;
$ Outstr = sprintf ("% d = % x hexadecimal or % o octal \ n", $ num );
Print ($ outstr );
Output 26 = 1a hexadecimal or 32 octal
Iv. scalar conversion functions
Function Name: chop
Call syntax $ lastchar = chop (var );
Description var can be a variable or an array. When var is a variable, the last character is deleted and assigned to $ lastchar. When va
When r is an array/list, the last character of all elements is deleted, and the last letter of the last element is assigned to $ l.
Astchar.
Function Name chomp
Call syntax result = chomp (var );
Commentary checks whether the last character of the element in the string or string list is a line separator defined by the system variable $/
If yes, delete it. The return value is the number of characters actually deleted.
Function Name crypt
Call syntax result = crypt (original, salt );
Describe how to use the DES algorithm to encrypt strings. original is the string to be encrypted, and salt is the string of two characters,
Defines how to change the DES algorithm to make decoding harder. The returned value is an encrypted string.
Function Name hex
Call the syntax decnum = hex (hexnum );
Converts a hexadecimal number (in string form) into a decimal number.
Function Name int
Call the syntax intnum = int (floatnum );
Converts the rounded decimal part of a floating point number to an integer.
Function Name oct
Call the syntax decnum = oct (octnum );
Converts an octal number (in string form) or a hexadecimal number ("0x .." form) into a decimal number.
Function Name ord
Call syntax asciival = ord (char );
Returns the ASCII value of a single character, similar to the function of the same name in PASCAL.
Function Name chr
Call syntax $ char = chr (asciival );
Returns the corresponding characters of the ASCII value, similar to the functions with the same name in PASCAL.
Function Name pack
Call the syntax formatstr = pack (packformat, list );
The description converts a list or array
In a simple variable. The packformat parameter contains one or more format characters. Each element in the list corresponds to one,
Characters in different formats can be separated by spaces or tabs, because pack ignores spaces.
In addition to the formats a, A, and @, you can add an integer after the format is repeated multiple times. For example:
$ Twoints = pack ("i2", 103,241 );
Apply the same format to all elements with a * number, for example:
$ Manyints = pack ("I *", 14, 26, 11, 83 );
For a and A, the following integer indicates the length of the string to be created. The repeat method is as follows:
$ Strings = pack ("a6" x 2, "test1", "test2 ");
The format @ is special. An integer must be added after it. This number indicates the length required by the string. If the length is not long enough
Is supplemented by null, for example:
$ Output = pack ("a @ 6 a", "test", "test2 ");
The most common purpose of the pack function is to create data that can interact with the C program. For example, strings in the C language are empty characters (n
You can create such data as follows:
$ Cstring = pack ("ax", $ mystring );
The following table shows the equivalence relationship between some format characters and Data Types in C:
Character equivalent C Data Type
C char
D double
F float
I int
I unsigned int (or unsigned)
L long
L unsigned long
S short
S unsigned short
The complete format characters are shown in the following table.
Format Character Description
A string supplemented by null
A string supplemented by Space
B-Bit String, before Low Position
B-bit string with a high position in front
C-Signed characters (usually-128 ~ 127)
C unsigned characters (usually 8 characters)
D double-precision floating point number
F single-precision floating point number
H hexadecimal number string, low front
H hexadecimal number string with a high position in front
I-signed integer
I unsigned integer
L signed long integer
L unsigned long integer
N short integer in network order
N Network order long integer
P string pointer
S signed short integer
S unsigned short integer
Convert u to uuencode format
V VAX short integer
V vax ordinal long integer
X an empty byte
X returns one byte.
@ Fill with null bytes
Function Name unpack
Call syntax @ list = unpack (packformat, formatstr );
The unpack function is opposite to the pack function. The values stored in the machine format are converted into a list of Perl values. Its format characters
Similar to pack (that is, the preceding table), A converts the machine format string to A Perl string and removes it.
All spaces or null characters at the end of the string; x indicates skipping one byte; @ indicates skipping some bytes to the specified position. For example, @ 4 indicates skipping.
4 bytes. Here is an example of @ and X contract: $ longrightint = unpack ("@ * X4 L", $ pac
Kstring );
This statement converts the last four bytes into unsigned long integers. The following describes how to decode the uuencode file.
Example:
Copy codeThe Code is as follows :#! /Usr/local/bin/perl
Open (CODEDFILE, "/u/janedoe/codefile") |
Die ("Can't open input file ");
Open (OUTFILE, "> outfile") |
Die ("Can't open output file ");
While ($ line = <CODEDFILE> ){
$ Decoded = unpack ("u", $ line );
Print OUTFILE ($ decoded );
}
Close (OUTFILE );
Close (CODEDFILE );
When using pack and unpack for uuencode, remember that although they work with uuencode and uuudecode in UNIX
The tool has the same algorithm, but does not provide the first line and the last line. If you want to use uudecode to import the File Created by pack output
Line decoding. the first line and the last line must also be output (for details, see the uuencode help in UNIX ).
Function Name vec
Call the syntax retval = vec (vector, index, bits );
As the name implies, vec is a vector function, which treats the value of a simple variable vector as multiple pieces of (dimension) data,
Each block contains a certain number of BITs, which together constitute a vector data. Each call accesses a piece of data and can read
. The index parameter is like an array subscript. It indicates which part to access. 0 indicates the first part, and so on,
Note that the access order is from right to left, that is, the first block is on the far right. The bits parameter specifies the number of digits in each block.
Think 1, 2, 4, 8, 16 or 32.
Example
Copy codeThe Code is as follows :#! /Usr/local/bin/perl
$ Vector = pack ("B *", "11010011 ");
$ Val1 = vec ($ vector, 0, 4 );
$ Val2 = vec ($ vector, 1, 4 );
Print ("high-to-low order values: $ val1 and $ val2 \ n ");
$ Vector = pack ("B *", "11010011 ");
$ Val1 = vec ($ vector, 0, 4 );
$ Val2 = vec ($ vector, 1, 4 );
Print ("low-to-high order values: $ val1 and $ val2 \ n ");
Result: high-to-low order values: 3 and 13.
Low-to-high order values: 11 and 12
Function Name defined
Call the syntax retval = defined (expr );
Judge whether an element of a variable, array, or array has been assigned a value. Expr is the variable name, array name, or
Array elements.
If defined, true is returned. Otherwise, false is returned.
Function Name undef
Call the syntax retval = undef (expr );
Describe how to cancel definitions of variables, arrays, array elements, and even subprograms and reclaim their space. The returned value is always undefined.
, Which is equivalent to an empty string.
5. array and list Functions
Function Name grep
Call the syntax @ foundlist = grep (pattern, @ searchlist );
Similar to the UNIX search tool with the same name, the grep function extracts elements that match the specified pattern in the list. The parameter p
Attern is the pattern to be searched. The returned value is a list of matching elements.
Example @ list = ("This", "is", "a", "test ");
@ Foundlist = grep (/^ [tT]/, @ list );
Result @ foundlist = ("This", "test ");
Function Name splice
Call the syntax @ retval = splice (@ array, slipelements, length, @ newlist );
The commenting concatenation function can insert an element into the List (array), delete a sublist, or replace a sublist. Parameter ski
Pelements is the number of elements skipped before splicing, length is the number of elements to be replaced, newlist is to be spliced in
. When the length of newlist is greater than the length, the subsequent elements are automatically moved backward, and vice versa. Therefore,
When length = 0, it is equivalent to inserting an element into the list, which is like a statement.
Splice (@ array,-1, 0, "Hello ");
Add an element to the end of the array. When newlist is empty, it is equivalent to deleting the sublist. If length is empty
Delete all elements from the skipelements column, and delete the last element as: splice (@ array,
-1); in this case, the returned value is the list of deleted elements.
Function name shift
Call the syntax element = shift (@ arrayvar );
Describe how to delete the first element of the array, move the remaining element forward, and return the deleted element. When no parameter is added @
ARGV.
Function Name unshift
Call the syntax count = unshift (@ arrayver, elements );
Opposite to shift, add one or more elements at the beginning of array arrayvar, and return the result (list)
. Equivalent to splice (@ array, 0, 0, elements );
Function Name push
Call the syntax push (@ arrayvar, elements );
Add one or more elements to the end of the array. Equivalent to slice (@ array, @ array, 0, elements );
Function name pop
Call the syntax element = pop (@ arrayvar );
The comment is opposite to push. Delete the last element of the list and use it as the return value. If the list is empty, return
"Undefined value" (that is, an empty string ).
Function Name split
Call syntax @ list = split (pattern, string, maxlength );
Describe how to split a string into a list of elements. Each time pattern is matched, a new element is started, but patt
The ern itself is not included in the element. Maxlength is optional. When it is specified, it is not separated when it reaches this length.
Function Name sort
Call the syntax @ sorted = sort (@ list );
The comments are sorted in alphabetical order.
Function Name reverse
Call the syntax @ reversed = reverse (@ list );
The comments are sorted in alphabetical order.
Function Name map
Call the syntax @ resultlist = map (expr, @ list );
This function is defined in Perl5. Each element in the list can be used as the operand of the expression expr.
And the result is returned as the return value. In the expression expr, the system variable $ _ represents each element.
Example 1: @ list = (100,200,300 );
@ Results = map ($ _ + 1, @ list );
2. @ results = map (& mysub ($ _), @ list );
Result 1: (101,201,301)
2. None
Function Name wantarray
Call syntax result = wantarray ();
In Perl, the behavior of some built-in functions varies depending on whether simple variables are processed or arrays, such as chop. Custom
Sub-programs can also define these two kinds of behavior. This function returns a non-zero value when the subroutine is expected to return a list.
Value (true); otherwise, the value is zero (false ).
Example
Copy codeThe Code is as follows :#! /Usr/local/bin/perl
@ Array = & mysub ();
$ Scalar = & mysub ();
Sub mysub {
If (wantarray ()){
Print ("true \ n ");
} Else {
Print ("false \ n ");
}
}
Result $ program
True
False
$
6. Associate array Functions
Function Name keys
Call the syntax @ list = keys (% assoc_array );
Returns the unordered subscript list of the associated array.
Function Name values
Call syntax @ list = values (% assoc_array );
Returns a list of unordered values of the joined array.
Function Name: each
Call syntax @ pair = each (% assoc_array );
Returns the list of two elements-key-value pairs (I .e. subscripts and corresponding values), which are also unordered. When the joined array is empty,
Returns an empty list.
Function Name delete
Call the syntax element = delete (assoc_array_item );
Describe how to delete an element from an associated array and use its value as the return value.
Example % array = ("foo", 26, "bar", 17 ");
$ Retval = delete ($ array {"foo "});
Result $ retval = 26;
Function name exists
Call the syntax result = exists (element );
The description is defined in Perl5 to determine whether an element exists in the associated array. If so, a non-zero value (true) is returned. Otherwise
Returns NULL (false ).
Example $ result = exists ($ myarray {$ mykey });