Command :#
Notes: remark announcement
Example: # This program is a demo Annotation
-------------------------------------------------------------------------
Command: Print
Syntax 1: Print filehandle list
Note: This filehandle serves as a bridge between I (input) and O (output). You can use filehandle to read and write data. Stdin indicates the input data from which side, for example, the input data from the computer's keyboard, stdout indicates the output data from which side, for example, the output data from the computer's screen, and stderr indicates the output of wrong data from which side, for example, output from a computer screen. In Perl, there are three standard filehandle:
(1) stdin (standard input): filehandle representing 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, you need to use the open function to open a filehandle. We can use the print function to output the list data to filehandle. Before introducing the print function, let's take a look at the special print characters in the print function:
Inner Meaning
N newline
R cursor line feed return
T Tab key
F form feed
B. Return one-click backspace
V vertical Tab key
A bell
07 ASCII code
XFF hexadecimal code
C [control characters
Example:
Print stdout "I love perln ";
Add "I love Perl" with a line break to display on the screen.
-------------------------------------------------------------------------
Syntax 2: Print list
NOTE: If filehandle is omitted, it is set to stdout in filehandle. That is, the data content of the list is displayed on the screen.
Example:
$ Str1ng = "Perl ";
Print "I love $ stringn ";
"I love Perl" is added with a line break and displayed on the screen. to invalidate the variable in double quotation marks, you can add this symbol before the variable. For example, if print "I love $ string"; is used, the string "I love $ string" is displayed.
-------------------------------------------------------------------------
Syntax 3: Print
NOTE: If both filehandle and list are omitted, the stdout is used as filehandle and the $ _ variable is output. If the $ _ variable is a Null String, an empty string is displayed.
Example:
$ _ = "I love perln ";
Print;
The "I love Perl" and the line feed will be displayed on the screen.
-------------------------------------------------------------------------
Command: printf
Syntax 1: printf filehandle list
Note: the syntax of printf in C language is also provided in Perl. Its usage is the same as that in C language. If you omit filehandle, std0ut will also be regarded as a set filehandle. Before introducing the printf function, let's take a look at the characters that transform symbols in the printf function:
Inner Meaning
% C characters
% S string
% D integer
% F floating integer
% H hexadecimal code
% O octal code
Example:
Printf <"chmod % d % s \ n", "7l1", "cgi ");
The "chmod 7ll cgi" is added with a line break and displayed on the screen.
-------------------------------------------------------------------------
Command: Chop
Syntax: Chop ($ string)
Note: Delete the last character. I often use this function to break the line feed? BR>? N) delete it.
Example:
$ String = "Hello! N ";
Chop ($ string); # $ string = "Hello! ";
The two lines can also be written as chop ($ string = "Hello! \ N ");
-------------------------------------------------------------------------
Command: Split
Syntax: Split (/pattern/, $ string, limit)
/Pattern is the text processing mode. The syntax is described in detail in the next section. Limit indicates the number to be split, which can be omitted.
Note: Use a specified text processing mode to split the $ string.
Example:
$ String = "I; am; cute ";
@ List = Split (//, $ string); # @ list = <"I", "am", "cute ");
($ A, $ B, $ c) = Split (//, $ string); # $ A = "I", $ B = "am ", $ c = "cute ";
@ List = Split (/:/, $ string, 2); # @ list = ("I", "love ");
When sending CGI application data, the data is first encoded, and the data content of each data field in form is separated by the & symbol, therefore, each data field must be separated by the & symbol during decoding. For example:
$ String = "Who = A & Email = B ";
@ List = Split (//, $ string), # @ list = <"Who = A", "email = 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:
$ List = "Who = ";
(0 $ name, $ value) = sp1it (/=/, $ list); # $ name = "who"; $ value = "";
-------------------------------------------------------------------------
Command: Keys
Syntax: Keys (% array)
Description: Retrieves all keys in the associated array % array.
Example:
% Names = (1, "one", 2, "two ");
@ List = keys (% names), # @ list = (L, 2 );
-------------------------------------------------------------------------
Command: Values
Syntax: values (% array)
Description: extracts all values in the associated array % array.
Example:
% Names = (1, "one", 2, "two ");
@ List = values (% names); # @ list = ("one", "two ");
-------------------------------------------------------------------------
Command: reverse
Syntax: reverse (@ array)
Note: sorts the elements in the array @ array from the back to the front.
Example:
@ List = ("A", "B", "C", "D ");
@ 1ist = reverse (@ list); # @ list = ("D", "C", "B", "")
-------------------------------------------------------------------------
Command: Sort
Syntax: Sort (@ array)
Note: The elements in the array @ array are sorted from small to large. to sort the elements from large to small, add the reverse function.
Example:
@ Array = ("B", "C", "");
@ Array = sort (@ array); # @ array = ("A", B "," C ");
@ Array = (reverse sort @ array); # @ array = ("C", "B", "");
This syntax can also be written as @ array = (reverse sort (@ array ));
@ Number = (l0, 3.20 );
@ Number = sort (@ number); # @ number = (l0, 20,3 );
From the above example, we can know that if the sort function is required to sort values in order, an error will occur on a business trip. Therefore, we can use the following method to sort values using the sort function correctly.
@ Number = (sort {$ A <=> $ B} @ number); # @ number = (, 20 );
-------------------------------------------------------------------------
Command: Length
Syntax: length ($ string)
Description: calculates the bytes value of a string $ string.
Example:
$ String = "Perl ";
@ Size = length ($ string); # $ size = 4;
-------------------------------------------------------------------------
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, the specified string is retrieved from the right of the string.
Description: extracts the desired string from a string $ string.
Example:
$ X = substr ("testing", 2, 2); # $ x = "St ";
$ X = substr ("testing", 2); # $ x = "sting ";
$ X = substr ("testing",-2, 2); # $ x = "in ";
-------------------------------------------------------------------------
Command: Index
Syntax: Index ($ string, $ substring, position)
$ Substring indicates 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: return the position of the character to be searched in a string $ string. If no character is found in the string, the value-L is returned.
Example:
$ X = index ("testing", "T"); # $ x = 0;
$ X = index ("testing", "T", 2); # $ x = 3;
$ X = index ("testing", "Perl"); # $ x =-L;
-------------------------------------------------------------------------
Command: Push
Syntax: Push (@ array, $ string)
Note: add the new element string to the array @ array after the last element of the array @ array.
Example:
@ Array = ("one", "two ");
Push (@ array, "three"); # @ array = <"one", "two", "three ");
-------------------------------------------------------------------------
Command: Pop
Syntax: Pop (@ array)
Note: Delete the last element of the array @ array and return the deleted element.
Example:
@ Array = <"one" '"two ");
$ Rm = pop <@ array); # @ array = ("one"); and $ Rm = "two ";
-------------------------------------------------------------------------
Command: unshift
Syntax: unshift (@ array, $ string)
Note: add $ string to the array @ array before the first element of the array.
Example:
@ Array = ("one", "two ");
Unshift (@ array '"three"); # @ array = ("three", "one", "two ");
-------------------------------------------------------------------------
Command: Shift
Syntax: shift (@ array)
Note: 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)
Note: add the specified character $ string to the element of an array @ array and return the result.
Example:
@ Array = ("one", "two", "three ");
$ Total = join (":", @ array); # $ Total = "One: Two: Three ";
-------------------------------------------------------------------------
Command: grep
Syntax: grep (/pattern/, @ array)
Note: Find the array elements that match the regular expression.
Example:
@ Array = ("one", "on", "in ");
$ COUNT = grep (/on/, @ array); # $ COUNT = 2 at this time; (representing that two elements match)
@ Result = grep (/on/, @ array); # @ result = ("one", "on ");
-------------------------------------------------------------------------
Command: Hex
Syntax: Hex ($ string)
Note: Convert the hexadecimal value to decimal.
Example:
$ Decimal = hex ("FF"); # $ decimal = 255;
-------------------------------------------------------------------------
Command: Rand
Syntax: rand ($ interger)
Note: It is often used with the function srand to obtain a random number. If the stand function is not declared in advance, the retrieved 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); # $ int value is greater than 0 and less than 10
If you want to generate a random number that is an integer, you must add the int function.
$ Int = int (RAND (10); # the value of $ int is an integer between 0 and 9.
-------------------------------------------------------------------------
Command: localtime
Syntax: localtime (time)
Note: 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 indicates the number of seconds [] $ min indicates the score []
$ Hour indicates the hour [] $ mday indicates the day of the month []
$ Mon indicates the number of months [0, 11]. You must add $ Mon to L to conform to the actual situation.
$ Year: the number of years from 990
$ Wday indicates the day of the week from Saturday []
$ Yday indicates the day of the year [0,365].
$ Isdst is just a flag
After knowing these variables, you can apply them 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 date" command. The reader should be reminded that the UNIX system's external program can be executed with the 'symbol. If the' symbol is used (single quotation marks), the system program cannot be correctly executed.
$ Date = '/usr/bin/date ';
In perl5, you can use the following command to obtain the system time.
$ Date = localtime (time );
-------------------------------------------------------------------------
Command: Die
Syntax: Die list
Note: The list string is displayed and the program exits. Often and $! This indicates that the variables of the error message are used together.
Example:
Open (File '"$ FILENAME") | die "cannotopenfile $! N ";
If the file fails to be opened, an error message is displayed, and then the program is exited.
---------------------------------------------------------------------
Command: Open
Syntax: open (filehandle' "$ fiiename ")
$ Filename is a specified file name.
Note: This is a very common function that can be used to open a file (read0niy ). 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 (inpnt) and O (output). filehandle can be used to read and write data. You can use the open function to open a specified file. Then you can use <filehandle> 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 CGI program writing, you must add the absolute path name of the file before opening the file.
Example:
$ Filename = vpath/cgi.txt ";
Open (File '"$ FILENAME") | die "can not open $ filename \ n ";
# Assign data to the pure variable $ line (one row and one row)
While ($ line = <File> ){
Print "$ line ";
}
Close (File );
The contents of the cgi.txt file will be displayed.
-------------------------------------------------------------------------
Syntax 2: open (filehandle, "<$ FILENAME ")
Note: This syntax can also open an existing file (read only ).
Example:
$ Filename = "/path/cgi.txt ";
Open (file, "<$ FILENAME") | die "can not open $ filenamen ";
# Assign all <File> data content to the array @ Array
@ Array = <File>;
Close (File );
Print "@ array ";
The contents of the cgi.txt file will also be displayed.
-------------------------------------------------------------------------
Syntax 3: open (filehandle, "> $ filemme)
Note: Create a new file (write only). If the file already exists, it overwrites the old file name. You can use print filehandle to write data to the opened file.
Example:
$ Filename = "/path/cgi.txt ";
Open (file, "> $ FILENAME") | die "can not open $ filenamen ";
Print file "this is a new line1n"; # N is a newline character.
Print file "this is a new line2n ";
Close (File );
The data is stored in a new file.
-------------------------------------------------------------------------
Syntax 4: open (filehandle, "> $ FILENAME ")
Note: Data is written to a file (write only) by appending. If the specified file name does not exist, a new file will be created.
Example:
$ Filename = "/path/cgi.txt ";
Open (file, ">>$ FILENAME") | die "can not open $ filenamen ";
Print file "This Is A newline1n ";
Print file "This Is A newline2n ";
Close (File );
The data is appended to a file (cgi.txt.
-------------------------------------------------------------------------
Syntax 5: open (filehandle, "| Unix Command ")
Note: The data in fiiehandle is output to Unix commands for processing.
Example:
$ Mailprog = "/usr/UCB/mail"; # Add absolute paths to Unix systems !)
$ Who = "jcjung@tem.nctu.edu.tw ";
Open (file, "| $ mailprog $ who") | die "cannotfail! N ";
Print file "do you want me? N ";
Print file "a little faster! 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 CGL application that has been criticized and instructed by a letter. The next chapter in this book will provide a detailed introduction.
-------------------------------------------------------------------------
Command: Close
Usage: Close (filehandle)
Note: After you use the open function to open a filehandle, you must use the close function to close the opened filehandle.
Example:
Open (fiiehandle, "$ FILENAME ");
Close (filehandle );
-------------------------------------------------------------------------
Command: Pack
Syntax: Pack ("specified format", list)
Note: The Pack function converts 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 = "";
Convert the ASCII code 65 to an unsigned character, where C indicates that the character is to be converted to an unsigned character.
-------------------------------------------------------------------------
Command: Read
Syntax: Read (filehandle, $ string, length)
Length indicates the length (bytes) of the read string ). Note: Use the READ function to read data in filehand1e 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, and then assigned to the $ buffer variable.
-------------------------------------------------------------------------
Command: Exit
Syntax: Exit
Description: exit the program.
Example:
Print "I love Perl ";
Exit;
After "I love Perl" is displayed, exit the program immediately.