Common Perl Functions
Command: Print
Syntax: Print filehandle list
Note: 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, input from the keyboard of a computer; stdout indicates the connection from which data is output; for example, output 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: remark announcement
Example: # This is a description
--------------------------------------------------------------------------------
Command: Print
Syntax: Print filehandle list
Note: 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, input from the keyboard of a computer; stdout indicates the connection from which data is output; for example, output 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
NOTE: 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
NOTE: If filehandle and list are omitted, the filehandle of stdout will be taken as the filehandle, and the data content of the variable $ _ will be output. if the $ _ variable is a Null String, an empty string is displayed.
Example: $ _ = "\ n"; print; a line break is added to "" and displayed on the screen.
--------------------------------------------------------------------------------
Command: printf
Syntax: printf filehandle list
Note: the syntax of printf in C language is also mentioned in Perl. Its usage 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"); adds a line break to chmod 711 CGI and displays it on the screen.
--------------------------------------------------------------------------------
Command: Chop Syntax: Chop ($ URL)
Note: 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.
Note: 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 ");
Sending CGI applicationsProgramData 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: Retrieves 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: extracts all values in the associated array % array.
Example: % names = (1, "Mike", 2, "Michael"); @ readval = values (% names); # @ readval = ("Mike ", "Michael ");
--------------------------------------------------------------------------------
Command: reverse
Syntax: reverse (@ array)
Note: 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)
Note: sort the elements in the array from small to large. If you want 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: obtains the byte (bytes) value of the string $ string.
Example: $ string = "perl5"; $ size = length ($ string); # $ size = 5;
--------------------------------------------------------------------------------
Command: substr
Syntax: substr ($ string, offset, length) offset represents the position of the starting character, length represents 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 the search starts. If the position is omitted, the search starts 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-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)
Note: Add a new element ($ string) to the array @ array at the end 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 "); $ Rm = "two ";
--------------------------------------------------------------------------------
Command: unshift
Syntax: unshift (@ array, $ string) Description: attaches 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)
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); then $ Total = "One: Two: Three ";
--------------------------------------------------------------------------------
Command: grep
Syntax: grep (/pattern/, @ array)
Note: 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)
Note: Convert the hexadecimal value to decimal.
Example: $ decimal = hex ("FF"); then $ decimal = 255;
--------------------------------------------------------------------------------
Command: Rand
Syntax: rand ($ interger)
Note: This function is often used with the srand function to obtain a random number. If the stand function is not declared first, the 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)
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 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
Note: The list string is displayed and the program exits. 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.