Perl programmatic functions

xiaoxiao2021-03-06  74

Quote:

Perl programming function instructions: # Description: Note: Name: # This is a note instruction instruction: Print Syntax: Print FileHandle List Description: This FileHandle can be seen as I (Input) / O (Output) A bridge between the data can be used to make data read. Screen output; stderr is a data representative from which to output errors, such as from the computer's screen output. Three standard filehandle: 1.stdin (standard input) in the Perl language: is a fileHandle 2.stdout representing stdin (standard output : Is a FileHandle 3.Stderr represented by Stdout: It is a FileHandle representing stderr. If you want to use other filehandle, use the Open this function to open a fileHandle, we can use the Print this function list data output Give FileHandle. Let's take a look at this function to see the special print characters in the print function: symbols its effects / n waters new line / R cursor wrap Return / T Tab key / F change form feed / B Return one / V vertical Tab key / a ring Bell / E ESCAPE button 7 decimal ASCII code / XFF Hexadecimal code / C [Control character example: print stdout "football world / N"; Plus "Football World" plus the wrap on the screen. Syntax: Print List Description: If FileHandle is omitted, the filehandle is kept For stdout. That is, the List's data content will be displayed on the screen. Example: $ URL = "www.netese.net/~peishan"; Print "Football World $ URL / N"; will appear on the screen "football World www.netese.net/~peishan $ URL "Syntax: Print Description: If FileHandle and List are omitted, it will be filehandle with stdout, and the data content of the _ 内 输 输 输 variable will be output. If $ _ variable is an empty string, it will be displayed. An empty string. Example: $ _ = "Football World / N"; Print; will "Football World"

Plus the wrap display on the screen ----------------------------------------- ------------------------------------- Command: PrintF Syntax: Printf FileHandle List Description: In Perl The syntax, usage and C language in the C language are also exactly the same. If the FileHandle is omitted, the stdout will be the FileHandle. Before introducing the Printf function, let us look Look at the characters of the transform symbol in the printf function. Symbols It effects% c character% S Strike% D integer% F floating diagram% H hexadecimal code% o eight into code example: printf ("chomod% D% S / N "," 711 "" CGI "); will display the CHMOD 711 CGI to the screen. -------------------------------------------------- ------------------------------ Command: Chop syntax: Chop ($ URL) Description: Delete the last character. Example: $ URL = "Linux.cqi.com.cn/~milan/"; Chop ($ URL); At this time, $ URL = "Linux.cqi.com.cn/~milan" and these two lines can also be written into ChOP ($ URL = "Linux.cqi.com.cn/~milan/"); ------------------------------- ------------------------------------------------ instruction : Split syntax: split (/ pattern /, $ text, limited) where / patter is a pattern of text processing, and Limit is representative of the number to be divided, usually omitted. Description: Use a specified text processing mode to split $ TEXT strings.

Example: $ text = "michael, gevin, mike"; @ name = split (/, /, $ text); # @Name = ("Michael", "Gevin", "Mike"); ($ A, $ b, $ c) = split (/, /, $ text); # This time $ a = "michael"; $ b = "gevin"; $ c = "mike"; @ name = split (/, $ String, 2); # This time @Name = ("Michael", "GEVIN"); encoding the data first, where the data content in the FORM is used & This symbol is separated, so it is necessary to divide each data field in a character that is divided by & this symbol when decoding. For example: $ text = "Mike = a & michael = b"; @ name = split (/ & /, $ text); # @Name = ("Mike = a", "michael = 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: $ name = "Mike = Michael ($ Name1, $ Name2) = Split (/ = /, $ list); # $ name1 = "mike"; $ name2 = "michael"; ------------- -------------------------------------------------- ----------------- Command: Keys Syntax: Keys (% array) Description: Remove all KEY in the associated array% Array. Example:% Name = (1, "Mike", 2, "Michael"); @ readkey = keys (% names); # @ Readkey = (1, 2); ---------- -------------------------------------------------- -------------------- Command: VALUES Syntax: VALUES (% array) Description: Remove all Value in the associated array% Array.

Example:% name = (1, "mike", 2, "michael"); @ readval = values ​​(% names); # @readval = ("Mike", "michael"); ------ -------------------------------------------------- ------------------------ Directive: Reverse Syntax: Reverse (@Array) Description: Re-arrange the elements in the array @Array. Example: @Back = ("a", "b", "c", "d", "e"); @ back = Reverse (@Back); # @Back = ("e", "d" , "C", "B", "A"); ------------------------------------ -------------------------------------------- instructions: sort syntax: sort (@Array) Description: Put the elements in the array from a small to large, if you want to be sorted by large to small, add the REVERSE this function. Example: @ABC = ("D", "B", "C", "A"); @ ABC = Sort (@ABC); # @abc = ("a", "b", "c" , "D"); @ ABC = (Reverse Sort @ ABC); # @ABC = ("D", "C", "B", "A"); this syntax can also be written @ ABC = (Reverse sort (@abc)); @ number = (5, 2, 10); @ number = sort (@Number); When the above example is sorted by the sort function, the error is wrong, so it is necessary to use the following to the following sentence. . @ Number = (sort @ number); # @ Number = (2, 5, 10); --------------------------- -------------------------------------------------- --- Command: Length Syntax: Length ($ String) Description: Stroke the string $ string byte (BYTES) value.

Example: $ string = "perl5"; $ size = length ($ string); # $ size = 5; ------------------------ -------------------------------------------------- ------ Command: Substr Syntax: Substr ($ String, Offset, Length) Offset represents the position of the start character, the length of the referenced string, if you omit the last value to the string from the start value to the string A character length. If Offset If is a negative value, the specified character will start from the right side of the string. Example: $ s = SUBSTR ("Perl5", 2, 2); # This time $ s = "rl"; $ s = substr ("Perl5", 2); # = $ s = "rl5"; $ s = SUBSTR ("Perl5", - 2, 2); # $ S = "ER"; --------------------------- -------------------------------------------------- --- Directive: Index Syntax: INDEX ($ String, $ Substring, Position) $ substring is the character to be looking for; the position represents which position starts to find, if the Position is omitted from the beginning. Note: Returns the characters you want to find in a string $ String, if you can't find characters in the string, the value is returned. Example: $ s = index ("Perl5", "P"); # = 0 $ s = index ("Perl5", "L", 2); # = $ s = 3 $ s = index ("Perl5", "Perl"); # = = -1 -------------------------------- ------------------------------------------------ instructions: Push Syntax: Push (@ Array, $ String) Description: At the last additional new element ($ String) to the array @Array in 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 Return the deleted element.

Example: @Array = ("One", "Two"); $ RM = POP (@Array); # @Array = ("one"); and $ rm = "two"; ------ -------------------------------------------------- ------------------------ Command: UNSHIFT Syntax: UNSHIFT (@ Array, $ String) Description: Additional in front of array @Array New elements $ string to array @Array. Example: @Array = ("One", "Two"); unshift (@Array, "three"); # @Array = ("Three", "One", "Two") ------ -------------------------------------------------- ------------------------ instruction: Shift Syntax: Shift (@Array) Description: Delete the first element of the array @Array and will be deleted Elements returned. Example: @Array = ("One", "Two"); @ rm = shift (@Array): # @Array = ("two"); and $ rm = "one"; ------ -------------------------------------------------- ------------------------ Directive: Join Syntax: Join ($ String, @ array) Description: Plus between the elements of a group @Array A specified character $ string and returns the result. Example: @Array = ("One", "Two", "Three"); $ TOTAL = JOIN (":", @ array); Total = "ONE: Two: Three"; ----- -------------------------------------------------- ------------------------- Command: GREP syntax: grep (/ pattern /, @ array) Description: Transferring the text processing mode (Regular Expression) Array elements come out.

Example: @Array = ("One", "on"); $ count = grep (/ on /, @ array); # $ count = 2 @ result = grep (/ on /, @ array ); # @Result = ("one", "on"); -------------------------------- ------------------------------------------------ instructions: HEX Syntax: HEX ($ String) Description: Turn a hexadecimal value into a decimal. Example: $ decimal = HEX ("ff"); at this time $ decimal = 255; ------------------------------- ------------------------------------------------ instruction : RAND Syntax: Rand ($ Interger) Description: The normal and function SRAND matching a random number, if the Stand function is not declared first, the retrieved constant value 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 declare the SRAND function can produce the effect of the 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 here Detail usage of this function. Example: ($ Sec, $ MIN, $ YEAR, $ WDAY, $ YDAY, $ ISDST) = localtime (time); where: $ sec represents the number of seconds [0,59] $ min Representative score [0,59] $ HOUR 小 小 [0,23] $ MDAY representative is the first few days in this month [1,31] $ mon represents the month [0,11], to add $ mon plus 1 After that, you can meet the actual situation. $ Year From 1990, $ WDAY is on Saturday, the representative is in the first few days in this week [0-6] $ yday counting from January day, the representative is the first year in this year. Day [0,365] $ ISDST is just a FLAG knows that these variables 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 system time with absolute path, if the absolute path is unclear, you can use the "Which Data" instruction.

Finally, if you want to mention the character, you can't correct the system. $ DATA = '/ usr / bin / data'; in the Perl5 version, you can also use the following line instruction to get the system time. $ DATA = localtime (Time); ------------------------------------------ -------------------------------------- Directive: DIE grammar: DIE List Description: Will put List The string is displayed and exits the program. Often and $! This represents the error message variable. Example: open (file, "$ filename") || DIE "Cannot open file $! / N; If the file failed to open the file, the error message will be displayed later. ---------------------- -------------------------------------------------- ---------------------- Directive: Open Syntax 1: Open (FileHandle, "$ filename") where $ filename is a file name specified open. Description: This is a very common function, which can be used for the ONLY. A file is often opened in the CGI program design, so the author will add this function related usage. This fileHandle can put it. It is seen as a bridge between I (Input) / O (Output), you can use FileHandle to make actions written out. You start to use the Open this function to open a specified file, then you can use To read the data content of the file opened, in the end, you must use a Close to turn the previously opened FileHandle. To note that in the CGI program, when using the Open function to open a file, you must Open the file before adding the absolute path name. Example: $ filename = "usr / abc.txt"; open (file, "$ filename") || DIE "Can't open the file $ filename / n; # # < File> Data Specifies to Pure Variable $ LINE (One Row and One Location) While ($ LINE = ) {Print "$ line";} close (file); will display the contents of this file. 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 = # 将 将 all data The content is specified to the array @Array close (file); print "@Array"; also displays the contents of the Abc.txt file.

Syntax 3: Open (FileHandle, "> $ filename") Description: Create a new file (WRITE ONLY), if this file already exists, you will overwrite the old file name. The data can be used in the open file in the way. Example: $ filename = "/ usr / abc.txt"; Open (file, "> $ filename") || DIE "Cannot open file $ filename / n; print file" this is a new line1 / n; # / n Is a wrap character print file "this is a new line2 / n; close (file); there is a new file in the data. Syntax 4: Open (FileHandle," >> $ filename ") Description: Data is added Enter a file (Write ONL) If the specified file name does not exist, it will create a new file. Sample: $ filename = "/ path / abc.txt"; open (file, ">> $ filename") | | DIE "Cannot open file $ filename / n"; Print file "this is a new line1 / n"; print file "this is a new line2 / n"; close (file); Append) to one Document (abc.txt). Syntax 5: Open (FileHandle, "| Unix Command) Description: You will proceed to the Directive of FileHandle to UNIX. Sample: $ mailprog =" / usr / ucb / Mail "; #unix the mail program (must add an absolute path) $ who =" tryfly@163.net "; $ Open (File," | $ mailprog $ who ") || DIE" Open failed / N "; Print file" i love you! / N "; Print file" I want to see you./n "; close (file); send file file FileHandle's data content to $ via UNIX system Mail WHO This variable is specified by the recipient. We can use the Open function to design a letter to criticize the CGI app, which will have a detailed introduction in the next chapter in this book. ----------- -------------------- ------------------------------------------------ instruction : Close Usage: Close (FileHandle) Description: After using this function to open a FileHandle, be sure to use the Close batch to close the open fileHandle.

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

New Post(0)