Perl common functions (suitable for beginners)!

zhaozj2021-02-16  50

Directive: Print

Syntax: Print FileHandle List

Note: This fileHandle can be seen as a bridge between I (Input) / O (Output), which can make data read from the action .Stdin is the representative input data, such as from the computer Keyboard input; stdout is the representative from which to output data; for example, from the computer's screen output; STDERR is a data representing which output error data, such as from the computer's screen output. Three standard filehandle: 1.stdin (Standard input): is a FileHandle representing stdin

2.stdout (standard output): is a FileHandle representing stdout

3.STDERR (Standard Error Output): Yes, if you want to use other filehandle, use Open this function to open a FileHandle, we can use the Print this function list to FileHandle.

Let's take a look at the special print characters in the print function before introducing this function for you.

instruction:#

Description: Comment symbol Remark announcement

Example: # This is an annotation description

-------------------------------------------------- ------------------------------

Directive: Print

Syntax: Print FileHandle List

Note: This fileHandle can be seen as a bridge between I (Input) / O (Output), which can make data read from the action .Stdin is the representative input data, such as from the computer Keyboard input; stdout is the representative from which to output data; for example, from the computer's screen output; STDERR is a data representing which output error data, such as from the computer's screen output. Three standard filehandle: 1.stdin (Standard input): is a FileHandle representing stdin

2.stdout (standard output): is a FileHandle representing stdout

3.STDERR (Standard Error Output): Yes, if you want to use other filehandle, use Open this function to open a FileHandle, we can use the Print this function list to FileHandle.

Let's take a look at the special print characters in the print function before introducing this function for you.

Symbol

/ N wrap New line

/ R cursor wrap Return

/ t tab key

/ f converted to form feed

/ b returned

/ V vertical Tab key

/ a ring Bell

/ e escape key

/ 007 decimal ASC II code

/ XFF hexadecimal code

/ c [control character

Example: print stdout "Online Academy / N"; Plus "Online Academy" plus the wrap on the screen.

Print List

Note: If FileHandle is omitted, the fileHandle is fixed to stdout. That is, the data content of the List is displayed on the screen.

Example: $ URL = "www.netease.net/~zmd";

PRINT "Online Academy $ URL / N";

"Online Academy www.netese.net/~zmd" will appear on the screen, if you want to make the variables in the double quotes fail, you can add "/" symbol in front of the variable. For example: Print "Online Academy / $ URL "; this shows:" Online Academy $ URL "

Print: print

Note: If FileHandle and List are omitted, it will be filehandle with stdout, and will output the data content of the _定 output variable. If the $ _ variable is an empty string, an empty string is displayed. Example : $ _ = "Online Academy / N"; Print; "Online Academy" plus the wrap display on the screen

-------------------------------------------------- ------------------------------

Printf

Printf FileHandle List

Note: In the Perl language, the syntax, usage and C language in the C language are also exactly the same. If the FileHandle is omitted, the stdout is the same as an internal filehandle. Introduce the Printf function to you, before introducing the printf function Let's take a look at the characters that transform symbols in the Printf function.

Symbol

% C character

% s string

% D integer

% F floating

% H hexadecimal code

% o eight into the code

Example: Printf ("Chomod% D% S / N", "711" "CGI"); will display the CHMOD 711 CGI to the screen.

-------------------------------------------------- ------------------------------

Directive: Chop Syntax: Chop ($ URL)

Description: Remove the last character.

Example: $ URL = "www.nease.net/~zmd/";

Chop ($ URL); At this time, $ URL = "www.nease.net/~zmd" and these two lines can also be written ($ url = "www.nease.net/~zmd/");

-------------------------------------------------- ------------------------------

Directive: Split

Syntax: split (/ pattern /, $ text, limited) where / pattern / is a pattern of text processing, and Limit is a number of numbers 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); # @Name = ("Michael", "gevin");

The data is encoded first in the transmission of the CGI application data, where the data content of the first data field in the FORM is separated from & this symbol, so when decoding, it is necessary to divide the characters in & this symbol. Each data field is divided. For example: $ text = "Mike = a & michael = b";

@ Name = Split (/ & /, $ text); # @Name = ("Mike = a", "michael = b"); and the name of the data field and the value of this data field is used = this symbol Separate, if you want to get the name of the data field and the corresponding value, use the data field to = this symbol, for example: $ name = "" Mike = Michael "; ($ Name1, $ name2) = Split (/ = /, $ list); # $ name1 = "mike"; $ name2 = "michael";

-------------------------------------------------- ------------------------------

Command: keys

Syntax: Keys (% array)

Description: Remove all of the KEY in the associated array% Array.

Example:% Name = (1, "Mike", 2, "Michael"); @ readkey = keys (% names); # this time @ iedkey = (1, 2);

-------------------------------------------------- ------------------------------

Directive: Values

Syntax: Values ​​(% array)

Description: Remove all VALUE in the associated array% Array.

Example:% name = (1, "mike", 2, "michael"); @ readval = values ​​(% name); # @readval = ("mike", "michael");

-------------------------------------------------- ------------------------------

Directive: Reverse

Syntax: Reverse (@Array)

Description: Reissue the elements in the array @Array to rearrange it before.

Example: @Back = ("a", "b", "c", "d", "e"); @ back = Reverse (@Back); # @Back = ("e", "d" , "C", "B", "A");

-------------------------------------------------- ------------------------------

Directive: sort

Syntax: sort (@Array)

Note: The elements in the array are sorted by small to large, if they are 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 into @ ABC = (Reverse Sort (@ABC)) @ Number = (5, 2, 10); @ number = sort (@Number); When the above example uses the sort function to sort the value, there is an error, so it is necessary to use the following. @ Number = (sort {$ a <=> $ b} @number); # @ Number = (2, 5, 10);

-------------------------------------------------- ------------------------------

Command: Length

Syntax: Length ($ String)

Description: Strue string $ string bytes value.

Example: $ string = "perl5"; $ size = length ($ string); # $ size = 5;

-------------------------------------------------- ------------------------------

Instruction: Substr

Syntax: Substr ($ String, Offset, Length) OFFSET represents the position of the start character, the length of the referenced string, if you omit, the length of the last character from the start value to the character string is omitted. 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); # = "rl";

$ S = SUBSTR ("Perl5", 2); # $ S = "rl5";

$ S = Substr ("Perl5", - 2, 2); # = "ER";

-------------------------------------------------- ------------------------------

Inducing: 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"); # $ S = 0

$ S = Index ("Perl5", "L", 2); # = 3 this time

$ S = Index ("Perl5", "Perl"); # = -1

-------------------------------------------------- ------------------------------

Directive: 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

Grammar: 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 add new elements $ String to array @Array. Example: @Array = ("One", "Two"); unshift (@Array, "three"); # @Array = ("Three", "One", "Two")

-------------------------------------------------- ------------------------------

Directive: Shift

Syntax: Shift (@Array)

Note: 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 $ string between the elements of a group @Array, and return the result.

Example:

@Array = ("One", "Two", "Three");

$ TOTAL = JOIN (":", @ array); at this time $ total = "one: two: three";

-------------------------------------------------- ------------------------------

Directive: GREP

Syntax: grep (/ pattern /, @ array)

Description: Find out the array elements of the REGLAR EXPRESSION.

Example:

@Array = ("One", "on", "in"); $ count = grep (/ on /, @ array); # $ count = 2

@ result = grep (/ on /, @ array); # @Result = ("one", "on");

-------------------------------------------------- ------------------------------

Directive: HEX

Syntax: HEX ($ String)

Explanation: Turn a hexadecimal value into a decimal.

Example: $ decimal = HEX ("ff"); $ decimal = 255;

-------------------------------------------------- ------------------------------

Directive: RAND

Syntax: Rand ($ Interger)

Note: The normal and function SRAND matching is obtained, and if the Stand function is not declared first, the remained 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 value will be greater than 0 and less than 10 If you want the arbitrarily, it is an int # function.

$ int = int (Rand (10)); # $ int value is an integer, and the value is between 0 and 9

-------------------------------------------------- ------------------------------

Directive: LocalTime

Syntax: LocalTime (Time)

Description: You can return nine-related time elements, which often use the system time when writing the CGI application, so you will introduce this function's usage.

Example:

($ Sec, $ MIN, $ HOUR, $ Mon, $ Year, $ WDAY, $ YDAY, $ ISDST) = LOCALTIME (TIME)

Among them: $ 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 representative Month [0,11], after adding $ MON to 1 to 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 a method of 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

DIE LIST

Description: You will display the list string and exit the program. Often and $! This represents the error message variable.

Example: open (file, "$ filename") || DIE "Cannot open file $! / N; If the file fails to fail, the error will be displayed, and then the program will be exited.

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

New Post(0)