List of common system functions
Command: Print
Syntax: Print Filehandle LIST
Description: This Filehandle can be seen as a bridge between I (INPUT) and O (OUTPUT). You can use FILEHANDLE to read and write data. STDIN indicates the connection from which data is input, for example, from the keyboard of a computer, STDOUT indicates the connection from which data is output, for example, from the screen of a computer; STDERR indicates the data from which an error is output, for example, from the computer's screen. in PERL, there are three standard FILEHANDLE: 1. STDIN (standard input): represents the FILEHANDLE of STDIN.
2. STDOUT (standard output): represents the FILEHANDLE of STDOUT.
3. STDERR (standard error output): represents the FILEHANDLE of STDERR. If you want to use another FILEHANDLE, OPEN the function to OPEN a FILEHANDLE, we can use the PRINT function to output the data in the LIST to FILEHANDLE.
Before introducing the PRINT function, let's take a look at the special PRINT characters in the PRINT function:
Command:#
Note:Comment symbol Remark announcement
Example: # This is a comment
Command: Print
Syntax: Print Filehandle LIST
Description: This Filehandle can be seen as a bridge between I (INPUT) and O (OUTPUT). You can use FILEHANDLE to read and write data. STDIN indicates the connection from which data is input, for example, from the keyboard of a computer, STDOUT indicates the connection from which data is output, for example, from the screen of a computer; STDERR indicates the data from which an error is output, for example, from the computer's screen. in PERL, there are three standard FILEHANDLE: 1. STDIN (standard input): represents the FILEHANDLE of STDIN.
2. STDOUT (standard output): represents the FILEHANDLE of STDOUT.
3. STDERR (standard error output): represents the FILEHANDLE of STDERR. If you want to use another FILEHANDLE, OPEN the function to OPEN a FILEHANDLE, we can use the PRINT function to output the data in the LIST to FILEHANDLE.
Before introducing the PRINT function, let's take a look at the special PRINT characters in the PRINT function:
Symbol
\ N newline
\ R cursor line feed return
\ T tab key
\ F form feed
\ B returns a grid
\ V vertical tab key
\ A Bell
\ E escape key
\ 007 decimal asc ii Code
\ Xff hexadecimal code
\ C [control characters
Example: Print STDOUT "\ n"; add a line break to "" and display it on the screen.
Syntax: Print LIST
Description: If Filehandle is omitted, the data in Filehandle is set to STDOUT. That is, the data in the LIST is displayed on the screen.
Example: $ Url = "www.netease.net /~ Zmd ";
Print "online school $ url \ n ";
"Online school www.netease.net /~ Zmd ", if you want to invalidate the variable in double quotation marks, you can add the" \ "symbol before the variable. for example: print "\ $ url"; then it displays:" $ url"
Syntax: Print
Description: If Filehandle and LIST are omitted, the value of STDOUT is Filehandle and the variable $ _ is output. if the $ _ variable is a Null String, an empty string is displayed.
Example: $ _ = "Online school \ n"; print; a line break is added to "online school" and displayed on the screen.
Command: Printf
Syntax: Printf Filehandle LIST
Description: The syntax of printf in C language is also introduced in perl. The syntax is the same as that in C language. if you omit Filehandle, STDOUT will also be regarded as a fixed Filehandle. before introducing the printf function, let's take a look at the characters that transform symbols in the printf function.
Symbol
% C characters
% S string
% D integer
% F floating integer
% H hexadecimal code
% O octal code
Example: Printf ("chomod % d % s \ n", "711" "cgi"); the chmod 711 cgi is added with a line break and displayed on the screen.
Command: Chop Syntax: chop ($ url)
Description: Delete the last character.
Example: $ Url = "www.nease.net /~ Zmd /";
Chop ($ url); $ url = "www.nease.net /~ Zmd "and the two lines can also be written as chop ($ url =" www.nease.net /~ Zmd /");
Command: Split
Syntax: Split (/pattern/, $ text, limit) Where/pattern/is the text processing mode, and limit is the number of parts to be separated, which can be omitted.
Description: Use a specified text processing mode to split the $ text string.
Example:
$ Text = "Michael, Gevin, Mike"; @ name = split (/,/, $ text); # @ name = ("Michael", "Gevin ", "Mike ");
($ A, $ B, $ c) = split (/,/, $ text); # $ a = "Michael"; $ B = "Gevin "; $ c = "Mike ";
@ Name = split (/,/, $ string, 2); # @ name = ("Michael", "Gevin ");
When sending CGI application data, the data is first encoded, and the data content of the first data field in FORM is separated by the & symbol, therefore, each data field must be separated by the & symbol during decoding. Example: $ text = "Mike = A & Michael = B ";
@ Name = split (//, $ text); # @ name = ("Mike = A", "Michael = B "); the data field name and the value of this data field are separated by the = symbol. If you want to obtain the data field name and the corresponding value, use the symbol "=" to separate data fields, for example, $ name = "" Mike = Michael "";
($ Name1, $ name2) = split (/=/, $ list); # $ name1 = "Mike"; $ name2 = "Michael ";
Command: Keys
Syntax: Keys (% array)
Description: Retrieve all keys in the associated ARRAY % ARRAY.
Example: % NAME = (1, "mike", 2, "michael"); @ readkey = keys (% NAMES); # @ readkey = (1, 2 );
Command: Values
Syntax: Values (% array)
Description: Retrieve all values in the associated ARRAY % ARRAY.
Example: % NAMES = (1, "mike", 2, "michael"); @ readval = values (% NAMES); # @ readval = ("mike ", "michael ");
Command: Reverse
Syntax: Reverse (@ array)
Description: Sorts the elements in the array @ array from the back to the front.
Example: @ Back = ("A", "B", "C", "D", "E"); @ back = reverse (@ back ); # @ back = ("E", "D", "C", "B", "");
Command: Sort
Syntax: Sort (@ array)
Description: Sorts the elements in the array from small to large. to sort the elements from large to small, add the reverse function.
Example:
@ Abc = ("d", "B", "c", "a"); @ abc = sort (@ abc); # @ abc = ("", "B", "c", "d ");
@ Abc = (reverse sort @ abc); # @ abc = ("d", "c", "B", ""); this syntax can also be written as @ abc = (reverse sort (@ abc ));
@ Number = (, 10); @ number = sort (@ number); in the preceding example, when the sort function is used to sort values, an error occurs, therefore, use the following sentence. @ Number = (sort {$ a <=> $ B} @ number); # @ number = (2, 5, 10 );
Command: Length
Syntax: Length ($ string)
Description: Obtain the byte (bytes) value of the string $ string.
Example: $ String = "Perl5"; $ size = length ($ string); # $ size = 5;
Command: Substr
Syntax: Substr ($ string, offset, length) offset indicates the position of the starting character, and length indicates the length of the referenced string. If length is omitted, it indicates the length of the last character from the starting value to the string. If offset is a negative value, it specifies a character from the right of the string.
Example:
$ S = substr ("perl5", 2,2); # $ s = "rl ";
$ S = substr ("perl5", 2); # $ s = "rl5 ";
$ S = substr ("perl5",-2, 2); # $ s = "er ";
Command: Index
Syntax: Index ($ string, $ substring, position) $ substring is the character to be searched. position indicates the position from which to start searching. If the position is omitted, It is searched from the beginning.
Description: Returns the position of the character to be searched in a string $ string. If no character is found in the string, the value-1 is returned.
Example:
$ S = index ("perl5", "p"); # $ s = 0
$ S = index ("perl5", "l", 2); # $ s = 3
$ S = index ("perl5", "perl"); # $ s =-1
Command: Push
Syntax: Push (@ array, $ string)
Description: Append a new element ($ string) to the array @ array at the end of array @ array.
Example: @ Array = ("one", "two"); push (@ array, "three"); # $ @ array = ("one", "two ", "three ")
Command: Pop
Syntax: Pop (@ array)
Description: Delete the last element of the array (@ array) and return the deleted element.
Example: @ Array = ("one", "two"); $ rm = pop (@ array); # @ array = ("one "); $ rm = "two ";
Command: Unshift
Syntax: Unshift (@ array, $ string) Description: add the new element $ string to the array @ array before the first element of the array @ array.Example: @ Array = ("one", "two"); unshift (@ array, "three"); # @ array = ("three", "one ", "two ")
Command: Shift
Syntax: Shift (@ array)
Description: Delete the first element of the array @ array and return the deleted element.
Example: @ Array = ("one", "two"); @ rm = shift (@ array); # @ array = ("two "); while $ rm = "one ";
Command: Join
Syntax: Join ($ string, @ array)
Description: Add the specified character $ string to the element of an array @ array and return the result.
Example:
@ Array = ("one", "two", "three ");
$ Total = join (":", @ array); then $ total = "one: two: three ";
Command: Grep
Syntax: Grep (/pattern/, @ array)
Description: Find the array elements of the regular expression.
Example:
@ Array = ("one", "on", "in ");
$ Count = grep (/on/, @ array); # $ count = 2
@ Result = grep (/on/, @ array); # @ result = ("one", "on ");
Command: Hex
Syntax: Hex ($ string)
Description: Convert the hexadecimal value to decimal.
Example: $ Decimal = hex ("ff"); then $ decimal = 255;
Command: Rand
Syntax: Rand ($ interger)
Description: It is often used with the function srand to obtain a random number. If the stand function is not declared first, the retrieved constant value is a fixed value. This syntax returns a value between 0 and $ interger. If $ interger is omitted, a value between 0 and 1 is returned.
Example:
Srand; # The srand function must be declared before a random number is generated.
$ Int = rand (10); # $ the int value is greater than 0 and smaller than 10. If you want to generate a random number that is an integer, you must add int # This function.
$ Int = int (rand (10); # the value of $ int is an integer between 0 and 9.
Command: Localtime
Syntax: Localtime (time)
Description: Nine time-related elements can be returned. The system time is usually used when writing CGI applications. Therefore, the usage of this function is described in detail here.
Example:
($ Sec, $ min, $ hour, $ mday, $ mon, $ year, $ wday, $ yday, $ isdst) = localtime (time );
Where: $ sec represents the number of seconds [] $ min represents the score [] $ hour represents the number of hours [] $ mday represents the day of the month [] $ mon represents the number of months [11 ], you must add $ mon to 1 to conform to the actual situation. $ Year indicates the number of years from January 1, 1990. $ wday indicates the day of the week [0-6] $ yday from January 1, January 1, it means that in the day of the year [0,365] $ isdst is just a flag that knows these variables and can be applied in the CGI application. In addition, you can use the following command to obtain the system time in a UNIX system. To avoid errors, we recommend that you use the absolute path method to obtain the system time. If the absolute path is unclear, you can use the "which data" command. The system program cannot be correctly executed if the characters are to be extracted. $ Data = '/usr/bin/data'; In perl5, you can use the following command to obtain the system time. $ Data = localtime (time );
Command: Die
Syntax: Die LIST
Description: Displays the LIST string and exits the program. Often and $! This indicates that the error message variables are used together.
Example: Open (FILE, "$ filename") | die "cannot open a FILE $! \ N; if the file fails to be opened, an error message will be displayed, and then exit the program.
Command: Open
Syntax 1: Open (filehandle, "$ filename") where $ filename is a specified open file name.
Description: This is a very common function that can be used to open a file (read only ). In CGI programming, a file is often opened to read data. Therefore, the author will detail the usage of this function. This filehandle can be seen as a bridge between I (INPUT) and O (OUTPUT). You can use FILEHANDLE to read and write data. Start to use the OPEN function to OPEN a specified file. Then you can use & ltfilehandle> to read the data content of the opened file, finally, you must use the close function to close the previously opened filehandle. Note that when using the OPEN function to OPEN a file in the CGI program, you must add the absolute path name of the file before opening the file.
Example:
$ Filename = "usr/abc.txt ";
Open (FILE, "$ filename") | die "cannot open the FILE $ filename \ n; # assign the & ltfile> data to the pure variable $ line (one line and one line)
While ($ line = & ltfile>)
{
Print "$ line ";
}
Close (file); the content of the abc.txt file will be displayed.
Syntax 2: Open (filehandle, "<$ filename ")
Description: This syntax can also open an existing file (read only ).
Example:
$ Filesname = "usr/abc.txt ";
Open (file, "<$ filename") | die "cannot open file $ filename \ n ";
@ Array = & ltfile> # specify all the data content of & ltfile> to the array @ array close (file );
Print "@ array"; the content of the abc. TXT file is also displayed.
Syntax 3: Open (filehandle, "> $ filename ")
Description: Create a new file (write only). If the file already exists, it overwrites the old file name. You can use print filehandle to upload data to the opened file.
Example:
$ Filename = "/usr/abc.txt ";
Open (file, "> $ filename") | die "cannot open the file $ filename \ n;
Print file "this is a new line1 \ n; # \ n is a newline character
Print file "this is a new line2 \ n;
Close (file); data is stored in a new file.
Syntax 4: Open (filehandle, ">>$ filename ")
Description: The data is appended to a file (write only). If the specified file name does not exist, a new file will be created.
Example:
$ Filename = "/path/abc.txt ";
Open (file, ">>$ filename") | die "cannot open the file $ filename \ n ";
Print file "this is a new line1 \ n ";
Print file "this is a new line2 \ n ";
Close (file );
The data will be appended (appendpoint to a file (abc.txt.
Syntax 5: Open (filehandle, "| unix command ")
Description: The data in filehandle is input to the unix Command for processing.
Example:
$ Mailprog = "/usr/ucb/mail"; # The sender program on the unix system (the absolute path must be added)
$ Who = "mqingyi@126.com ";
$ Open (file, "| $ mailprog $ who") | die "Opening failed \ n ";
Print file "I love you! \ N ";
Print file "I want to see you. \ n ";
Close (file );
The FILEHANDLE of FILE will be sent to the receiver specified by the $ who variable through the unix System mail Program. We can use the open function to design a program to criticize CGI applications, which will be described in detail in the next chapter in this book.
Command: Close
Usage: Close (filehandle)
Description: After you use the open function to open a filehandle, you must use the close batch function to close the opened filehandle.
Example:
Open (filehandle, "$ filename ");
Close (filehandle );
Command: Pack
Syntax: Pack ("specified format", list)
Description: Pack this function will convert a list to the specified binary data format. The pack function is used in the process of CGI program segmentation and decoding, so I will briefly introduce the usage of this function here.
Example: $ String = pack ("c", 65); # $ string = "a"; convert the ascii code 65 into an unsigned character, c Indicates the meaning of the character to be converted to unsigned.
Command: Read
Syntax: Read (filehandle, $ string, length) Where length represents the length of the read string (bytes ).
Description: Use the read function to read the data in filehandle according to the specified string Length and assign it to the $ string variable. When the cgi program is split and decoded, if the FORM transmission mode is set to POST, the transmitted data is set to standard input, therefore, the data content is specified to the filehandle of the stdin standard input, while the CGI Environment Variable $ env {'content _ length'} represents the length of the data content sent by the user, therefore, we need to use the read function to obtain the data content sent by the user.
Example: Read (stdin, $ buffer, $ env {'content _ length'}); the data in the stdin standard input filehandle is read according to the specified String length, then assign the variable $ buffer.
Command: Exit
Syntax: Exit
Description: Exit the program.
Example: Print "I love cgi \ n"; exit; after "I love cgi" is displayed, the program will exit.