Perl commonly used in CGI programming

xiaoxiao2021-03-06  99

Directive: # 说明 说明: 注解 符 Remark Declaration example: # This program is a demonstration note ------------------------------- ------------------------------------------ Directive: Print Syntax 1: Print FileHandle List Description: This FileHandle can view it as a bridge between I (Input) / O (Output), which can make the action written in data read in the use of FileHandle. STDIN is the input data from the computer, such as the keyboard input from the computer, and stdout is output data from which side is output; for example, from the computer's screen output; STDERR is a data representing the output error from the computer, such as the screen output from the computer. In the Perl language, there are three standard FileHandle: (1) stdin: It behaves stdin FileHandle (2) stdout: is a FileHandle representing Stdout (3) stderr (Standard Error Output): Yes When you use other FileHandle if you want to use other FileHandle, you can open a FileHandle with the Open, we can use the Print this function to output the LIST's data to FileHandle. Before introducing this function, let's take a look at the special print symbol character in the print function: Symbol Connotation N waters new line R cursor wrap Return T TB button F change page FORM feed B returns a button Backspace V vertical Tab Key a ring Bell 07 decimal ASCII code XFF hexadecimal code C [control character example: print stdout "i love perln"; add "i love perl" to the screen. -------------------------------------------------- ----------------------- Syntax 2: Print List Description: If FileHandle is omitted, you will definiterate FileHandle to Stdout. That is, the data content of the LIST will be displayed on the screen. Example: $ str1ng = "perl"; print "i love $ stringn"; will add "I love perl" to the screen, and if you want to invade the variables in the double quotes, you can add it. This symbol is on. For example, if it is Print "I Love $ String"; "I Love $ String" string is displayed. -------------------------------------------------- ----------------------- Grammar 3: Print Description: I omitly omit FileHandle and List, will be filehandle with stdout, and will output $ _ this The data content of the output variable. If the $ _ variable is an empty string, an empty string is displayed. Example: $ _ = "i love perln"; print; add "I love perl" plus the wrap on the screen ------------------- -------------------------------------------------- - Directive: Printf Syntax 1: Printf FileHandle List Description: In the Perl language, it also provides the syntax, usage and C language in the C language in the Perl language. If the FileHandle is omitted, the STD0UT will be the FileHandle.

Before introducing the printf function, let's take a look at the character of the transformed symbol in the printf function: the symbolical content% C character% S Strike% D integer% F float% h heel to code% O eight input code Example: Printf <"CHMOD% D% S \n", "7L1", "CGI"); will add "ChMOD 7ll CGI" to the screen. -------------------------------------------------- ----------------------- Directive: Chop Syntax: Chop ($ String) Description: Delete the last character, often use this function to put the wrap word ? Br>? n) delete it. Example: $ String = "Hello! N"; Chop ($ String); # This time $ string = "Hello!"; These two lines can also be written ($ string = "hello! \N"); -------------------------------------------------- --------------------- Directive: Split Syntax: Split (/ Pattern /, $ String, Limit) where / patter is the pattern of text processing, in the next section There will be a detailed grammar introduction. And LIMIT is the number of points to be divided, which can be omitted. Note: Use a specified text processing mode to split $ String strings. Example: $ String = "i; am; cute"; @ list = split (/: /, $ string); # @List = <"i", "am", "cute"); ($ A, $ b, $ c) = split (/: /, $ string); # This time $ a = "i", $ b = "am", $ c = "cute"; @ list = split (/: /, $ String, 2); # @List = ("I", "Love"); encoding the data before transmitting the CGI application data, where the data content of each data field in the Form will be used & This symbol is spaced apart, so in decoding, it is necessary to divide each data field in the decoding character. For example: $ String = "WHO = A & email = B"; @ list = split (/ & /, $ string), # @List = <"WHO = a", "email = b"); and data field The value of the name and this data field is separated by = this symbol. If you want to get the name of the data field and the corresponding value, use the symbol to divide the data field, for example: $ list = "WHO = a "(0 $ name, $ value) = sp1it (/ = /, $ list); # n =" who "; $ value =" a "; ------------ -------------------------------------------------- ----------- Command: Keys Syntax: Keys (% array) Description: Remove all KEY in the associated array% array.

Example:% names = (1, "one", 2, "two"); @ list = keys (% names), # @ list = (l, 2); ---------- -------------------------------------------------- ------------- Command: VALUES Syntax: VALUES (% array) Description: Remove all Value in the associated array% Array. Example:% names = (1, "one", 2, "two"); @ list = values ​​(% names); # @List = ("one", "two"); ------ -------------------------------------------------- ----------------- Command: Reverse Syntax: Reverse (@Array) Description: Re-arrange the elements in the array @Array. Example: @List = ("a", "b", "c", "d"); @ 1ist = reverse (@List); # @List = ("D", "c", "b" , "A") --------------------------------------------- ---------------------------- Directive: Sort Grammar: Sort (@Array) Description: Will the elements in the array @Array are small Large sorting, if you want to be sorted by large to small, you should add the Reverse this function. Example: @Array = ("b", "c", "a"); @ array = sort (@Array); # @Array = ("a", b "," c "); @ array = (Reverse Sort @ array); # @Array = ("c", "b", "a"); this syntax can also be written into @ array = (Reverse sort (@Array)); @ Number = (L0, 3.20); @ Number = sort (@Number); # this time @ number = (L0, 20, 3); By the previous example, if you want the sort function to sort the value, you will make a mistake, so it is The following method can be sorted using the Sort function correctly. @ Number = (sort {$ a <=> $ b} @Number); # @ Number = (3, 10, 20); ---------- -------------------------------------------------- ----------------- Directive: Length Syntax: Length ($ String) Description: Question of a string $ string BYTES value. Example: $ string = "perl"; @ Size = Length; # # s = 4; ---------------------------------- --------------------------------------- Command: Substr Syntax: Substr ($ String, Offset LENGTH) where OFFSET represents the position of the start character, the length of the leadth represents the string length, such as omitting the last character length from the start value to the character string.

If the Offset is negative, the specified string will be taken from the right side of the string. Description: Take the desired string in a string $ String. Example: $ x = substr ("Testing", 2, 2); # = "st"; $ x = Substr ("Testing", 2); # This time $ x = "sting"; $ x = SUBSTR ("Testing", - 2, 2); # = "in"; --------------------------- ---------------------------------------------- instructions: index grammar : INDEX ($ String, $ Substring, Position) where $ substring is the character you want to find, and the position represents which position begins to find, if you omit the position, you will start from the beginning. Description: Returns the position you want to find in a string $ String, if you can't find the character in the string, the -L is returned. Example: $ x = index ("Testing", "T"); # = 0; $ x = index ("Testing", "T", 2); # This time $ x = 3; $ x = INDEX ("Testing", "Perl"); # = -l; ----------------------------- -------------------------------------------- Directive: Push Syntax: PUSH (@ Array, $ String) Description: Additional new element string to array @Array after the last element of array @Array. Example: @Array = ("One", "TWO"); Push (@Array, "Three"); # @Array = <"one", "two", "three"); ----- -------------------------------------------------- ------------------ Directive: POP syntax: POP (@Array) Description: Delete the last element of the array @Array and returns the deleted element. Example: @Array = <"one" '"two"); $ RM = Pop <@Array); # @Array = ("one"); and $ rm = "two"; -------- -------------------------------------------------- ----------------- Directive: UNSHIFT Syntax: Unshift (@ Array, $ String) Description: Additional new elements in front of array @Array $ string to 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 returns the deleted element.

Example: @Array = ("One", "Two"); $ RM = Shift (@Array); # @Array = ("Two"), and $ rm = "one"; ------ -------------------------------------------------- ----------------- Directive: Join Syntax: Join ($ String, @ array) Description: Add a specified character between a group of @array elements $ String, And return the results. Example: @Array = ("One", "Two", "Three"); $ TOTAL = JOIN (":", @ array); # This time $ total = "one: two: three"; ------ -------------------------------------------------- ------------------- Command: GREP Syntax: GREP (/ Pattern /, @ array) Description: The array element that complies with the regular expression (Regular Expression) will be found. Example: @Array = ("One", "on", "in"); $ count = grep (/ on /, @ array); # at this time $ count = 2; (represents two elements) @Result = GREP (/ on /, @ array); # @Result = ("one", "on"); ----------------------- -------------------------------------------------- Instruction: HEX Syntax: HEX ($ String) Description: Turn the hexadecimal value into a decimal. Example: $ decimal = HEX ("ff"); # d d = 255; ------------------------------ ------------------------------------------- Directive: RAND grammar: Rand $ interger) Description: The normal and function SRAND matchs to get a random number. If there is no pre-declaration of the Stand function, the value taken is a fixed value. This grammar returns a value between 0 and $ INTERGER. If the $ INTERGER is omitted, a value of 0 and 1 will be returned. Example: SRAND; # To first declare the SRAND function, can generate the effect of random number $ INT = rand (10); # $ int for greater than 0 and less than 10 If you want to generate the mess, you must add INT This function $ INT = Int (Rand (10)); # $ int value is an integer, and the value is between 0 and 9 ------------------- -------------------------------------------------- ---- Command: LocalTime Syntax: LocalTime (TIME) Description: You can return nine-related time elements, which often use the system time when writing CGI applications, so this function will be described in detail.

Example: ($ Sec, $ MON, $ Year, $ WDAY, $ YDAY, $ ISDST) = localtime (time); where: $ sec represents the number of seconds [0,59] $ min Representative fraction [0,59] $ HOUR Agree number [0,23] $ mday representative is the first few days in this month [1,31] $ mon represents the month [0,11], to add $ MON plus L After that, it can be in accordance with the actual situation $ year from the year of the year. The first few days in this year [0,365] $ ISDST is just a FLAG knows these variables, it can be applied in the CGI application. In addition, you can use the following row instruction to obtain the system time under the UNIX system. In order to avoid errors, it is best to obtain the system time with the method of absolute path. If the absolute path is unclear, you can use "Which Date", this instruction. Finally, to remind the reader to use the `this symbol to execute an external program of the UNIX system. If you use '(single quotes) this word symbol, you cannot correctly perform the system. $ DATE = `/ usr / bin / date`; in Perl5 version, you can also use the following line instructions to obtain system time. $ DATE = localtime (time); ------------------------------------------ ------------------------------- Directive: DIE grammar: DIE List Description: You will display the List string and exit program. Often and $! This represents the variable of the error message. Example: Open (file '"$ filename") || DIE "cannotopenfile $! N"; if the file failed, the error message will be displayed, then exit the program. -------------------------------------------------- ------------------- Command: Open Syntax: Open (FileHandle '"$ FIIENAME") where $ FileName is a file name that is specified. Note: This is a very common function, which can be used for the Open Open (READ0NIY). A file is often opened in the CGI program design, so the author will add related usage of this function. This FileHandle can regard it as a bridge between I (InPnt) / O (Output), which can use FileHandle to make actions written in data read. Start using the Open function to open a specified file, then you can use to read the data content of the file opened, and finally use the Close this function to close the previously opened FileHandle. It should be noted that in the CGI program writing, when using the Open this function to open the file, be sure to add the absolute path name of the file before the open file. Example: $ filename = vpath / cgi.txt "; Open (file '" $ filename ") || DIE" Can not open $ filename\n "; # Assign data to the Pure Variable $ LINE (one line) While ($ line = ) {Print "$ line";} close (file); will display the contents of the CGI.txt file. ---------------- -------------------------------------------------- ------- Syntax 2: Open (FileHandle, "<$ filename") Description: This syntax can also open an existing file (Read Only).

Example: $ filename = "/ path / cgi.txt"; Open (file, "<$ filename") || DIE "can not open $ filenamen"; # 将 all data content assigned to array @Array @ array = ; close (file); print "@Array"; also displays the contents of the CGI.txt file. -------------------------------------------------- ----------------------- Grammar 3: Open (FileHandle, "> $ filemme) Description: Create a new file (Write Only), if there is already This file is overwriting the old file name. You can write the data to the open file in the way. Example: $ filename = "/ path / cgi.txt"; Open (file, "> $ FileName ") || DIE" can not open $ filenamen "; print file" this is a new line1n "; #n is a wrap character print file" this is a new line2n "; close (file); there is a new data Document. ----------------------------------------------- -------------------------- Grammar 4: Open (FileHandle, ">> $ filename") Description: Data is written in additional way File (WRITE ONLY) 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); Append to a file (cgi.txt). -------------------------------------------------- ---------------------- Syntax 5: Open (FileHandle, "| Unix Command") Description: Just output to UNIX instructions in FiieHandle Processing. Example: $ mailprog = "/ usr / ucb / mail"; #unix to add a absolute path on the system!) $ WHO = "jcjung@tem.nctu.edu.tw"; Open (File, "| | $ mailprog $") || DIE "cannotfail! N"; print file "Don't want me? N"; print file "To reply a little! N"; close (file); With UNIX system mail, send the file of the FileHandle to the received person specified by this variable. We can use the Open function to design a CGL app that comes to letter criticism, which will have a detailed introduction in the next chapter in this book.

转载请注明原文地址:https://www.9cbs.com/read-104528.html

New Post(0)