Home > php教程 > PHP开发 > Perl common system functions

Perl common system functions

黄舟
Release: 2016-12-16 13:59:36
Original
1081 people have browsed it

Command: PRint
Syntax: printFilehandleLIST

Explanation: This Filehandle can be regarded as a bridge between I(INPUT)/O(OUTPUT), and FILEHANDLE can be used to read and write data. STDIN represents Which connection to input data from, such as input from the keyboard of a computer; STDOUT represents the connection from which data is output; for example, output from the computer screen; STDERR represents the connection from which to output incorrect data, such as output from the computer screen. In PERL There are three standard FILEHANDLEs in the language: 1.STDIN (standard input): it is a FILEHANDLE that represents STDIN
2.STDOUT (standard output): it is a FILEHANDLE that represents STDOUT
3.STDERR (standard error output): it is a FILEHANDLE that represents STDERR if When you want to use other FILEHANDLE, you need to use the OPEN function to open a FILEHANDLE. We can use the PRINT function to output the LIST data to the FILEHANDLE.
Before introducing the PRINT function to you, let us first take a look at the PRINT function Special printing characters:

Command: #
Description: Comment symbol Remark declares
Example: #This is a comment description




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


Command: print
Syntax: printFilehandleLIST
Explanation: This Filehandle can be regarded as a bridge between I(INPUT)/O(OUTPUT) and can be made using FILEHANDLE The action of reading and writing data. STDIN represents the connection from which data is input, such as from the keyboard of a computer; STDOUT represents the connection from which data is output; such as output from the screen of a computer; STDERR represents the connection from which erroneous data is output. , such as output from the computer screen. There are three standard FILEHANDLEs in the PERL language: 1.STDIN (standard input): FILEHANDLE representing STDIN
2.STDOUT (standard output): FILEHANDLE representing STDOUT
3.STDERR( Standard error output): FILEHANDLE representing STDERR. If you want to use other FILEHANDLE, you must use the OPEN function to open a FILEHANDLE. We can use the PRINT function to output the LIST data to the FILEHANDLE.
I am introducing the PRINT function to you. Before, let us take a look at the special printing characters in the PRINT function:

symbol and its function
n newline newline
r cursor newline return
ttab key
f page change formfeed
b return one space
v vertical tab key
a ring Bell
eescape key
007 decimal ASCII code
xff hexadecimal code
c〔control character
Example: printSTDOUT "Online School n"; display "Online School" with a newline on the screen.

Syntax :printLIST
Note: If Filehandle is omitted, Filehandle will be defaulted to STDOUT. That is, the data content of LIST will be displayed on the screen.
Example: $url="www.netease.net/~zmd";
print "Online Academy $urln";
"Online Academy www.netease.net/~zmd" will appear on the screen. If you want to invalidate the variables in double quotes, you can add the "" symbol in front of the variables. .For example: print "Online Academy $url"; so it displays: "Online Academy $url"

Syntax: print
Explanation: If you omit Filehandle and LIST, STDOUT will be used as Filehandle, and $ will be output. _The data content of this default output variable. If the $_ variable is an empty string, an empty string will be displayed.
Example: $_="Online School n";print; will display "Online School n" "Garden" plus line breaks are displayed on the screen



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


Command: printf
Syntax: printfFilehandleLIST
Explanation: The syntax of printf in C language is also proposed in perl language, and its usage is exactly the same as that in C language. If Filehandle is omitted, STDOUT will also be regarded as the default Filehandle. I will introduce it to you. Before the printf function, let us first take a look at the characters that change signs in the printf function.

Symbols and their functions
c characters
s strings
d integers
f floating integers
h hexadecimal codes
o octal codes
examples :printf("chomoddsn","711""cgi"); will display chmod711cgi plus line breaks on the screen.



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


Command: chop Syntax: chop($url)
Description: Delete the last character.
Example: $url="www.nease.net/~zmd/";
chop($url); then $url="www.nease.net/~zmd" and these two lines can also be written as chop($ url="www.nease.net/~zmd/");



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


Command: split
Syntax: split(/pattern/,$text,limit) where /pattern/ is the word processing mode, and limit represents the number to be split, which can generally be omitted.
Description: Use a specified word processing mode to split the $text string.
Example:
$text="Michael,Gevin,Mike";@name=split(/,/,$text);#At this time @name=("Michael","Gevin","Mike");
( $a,$b,$c)=split(/,/,$text);#At this time $a="Michael";$b="Gevin";$c="Mike";
@name=split( /,/,$string,2);#At this time@name=("Michael","Gevin");
When transmitting CGI application data, the data will be encoded first. The data content of the first data field in the FORM will be separated by the & symbol. Therefore, when decoding, the & symbol must be used as the dividing character. Split out each data field. For example: $text="Mike=A&Michael=B";
@name=split(/&/,$text);#At this time @name=("Mike=A","Michael=B"); and the data field The name and the value of this data field are separated by the = symbol. If you want to get the name of the data field and the corresponding value, use the = symbol to separate the data field, for example: $name=""Mike =Michael"";
($name1,$name2)=split(/=/,$list);#At this time $name1="Mike";$name2="Michael";



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


Command: keys
Syntax: keys(array)
Description: Get all the keys in the associative array ARRAY.
Example: NAME=(1,"mike",2,"michael");@readkey=keys(NAMES);#At this time @readkey=(1,2);



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


Command: values
Syntax: values(array)
Description: Get all the values ​​in the associative array ARRAY.
Example: NAMES=(1,"mike",2,"michael");@readval=values(NAMES);#At this time @readval=("mike","michael");



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


Command: reverse
Syntax: reverse(@array)
Description: Replace the elements in the array @array Elements are rearranged from back to front.
Example: @back=("A","B","C","D","E");@back=reverse(@back);#At this time@back=("E","D ","C","B","A");



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


Command: sort
Syntax: sort(@array)
Description: Sort the elements in the array from small to large. If you want to sort from large to small, you need to add the reverse function.
Example:
@abc=("d","b","c","a");@abc=sort(@abc);#At this time@abc=("a","b"," c","d");
@abc=(reversesort@abc);#At this time @abc=("d","c","b","a");This syntax can also be written as@abc= (reversesort(@abc));
@number=(5,2,10);@number=sort(@number); When the above example uses the sort function to sort the values, an error will occur, so use the following to the following This sentence. @number=(sort{$a<=>$b}@number);#At this time@number=(2,5,10);



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


Command: length
Syntax: length($string)
Description: Find the bytes value of the string $string.
Example: $string="Perl5";$size=length($string);#At this time $size=5;



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


Command: substr
Syntax: substr($string,offset,length) offset represents the position of the starting character, length represents the length of the quoted string, if length is omitted, it represents The length of the string from the starting value to the last character. If offset is a negative value, characters will be specified starting from the right side of the string.
Example:
$s=substr("perl5",2,2);#At this time $s="rl";
$s=substr("perl5",2);#At this time $s="rl5" ;
$s=substr("perl5",-2,2);#At this time $s="er";



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


Command: index
Syntax: index($string,$substring,position)$substring is the character to be found; position represents the position to start searching from, if position is omitted Just start from scratch.
Description: Returns the position of the character you are looking for in a string $string. If the character is not found in the string, the value -1 will be returned.
Example:
$s=index("perl5","p");#At this time $s=0
$s=index("perl5","l",2);#At this time $s=3
$s=index("perl5","perl");#At this time $s=-1



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


Command: push
Syntax: push(@array,$string)
Description: Append a new element ($string) to the end of the array @array into the array @array.
Example: @array=("one","two");push(@array,"three");#At this time $@array=("one","two","three")



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


Command: pop
Syntax: pop(@array)
Description: Change the array ( @array) deletes the last element and returns the deleted element.
Example: @array=("one","two");$rm=pop(@array);#At this time @array=("one");and $rm="two";



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


Command: unshift
Syntax: unshift(@array,$string) Description: in Append the new element $string to the array @array before the first element of the array @array. Example: @array=("one","two");unshift(@array,"three");#At this time @array=("three","one","two")



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


Command: shift
Syntax: shift(@array)
Description: Change the array @array Removes the first element and returns the removed element.
Example: @array=("one","two");@rm=shift(@array);#At this time @array=("two"); and $rm="one";



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


Command: join
Syntax: join($string,@array)
Description: in Adds a specified character $string between the elements of an array @array and returns the result.
Example:
@array=("one","two","three");
$total=join(":",@array); then $total="one:two:three";



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


Command: grep
Syntax: grep(/pattern/,@array)
Description: Find the array elements that combine text processing mode (regular expression).
Example:
@array=("one","on","in");
$count=grep(/on/,@array);#At this time $count=2
@result=grep(/on /,@array);#At this time @result=("one","on");



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


Command: hex
Syntax: hex($string)
Description: Convert hexadecimal value to decimal.
Example: $decimal=hex("ff"); then $decimal=255;



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


Command: rand
Syntax: rand($interger)
Explanation: Often used in conjunction with the function srand to obtain a random number. If the stand function is not declared first, the constant value taken out is a fixed value. This syntax returns a value between 0 and $interger, or if $interger is omitted, a value between 0 and 1 is returned.
Example:
srand;#The srand function must be declared first to produce the effect of random numbers
$int=rand(10);#The value of $int will be greater than 0 and less than 10. If you want the generated random number to be an integer, you must Plus the int# function
$int=int(rand(10));#The value of $int is an integer, and the value is between 0 and 9



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


Command: localtime
Syntax: localtime(time)
Description: Nine elements related to time can be returned, when writing CGI applications The system time is often used, so the usage of this function will be introduced in detail here.
Example:
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
where: $sec represents the number of seconds [0,59 〕$min represents the fraction [0,59] $hour represents the number of hours [0,23] $mday represents the day of the month [1,31] $mon represents the number of months [0,11], you need to change $ Only after adding 1 to mon can it meet the actual situation. $year is the number of years since 1990. $wday is calculated from Saturday, representing the day of the week [0-6]. $yday is calculated from January 1, representing the day of the year. Days[0,365]$isdst is just a flag. Once you know these variables, you can use them in CGI applications. In addition, you can also use the following line of instructions to obtain the system time under UNIX systems. In order to avoid errors, it is best to use the absolute path method to obtain the system time. If the absolute path is not clear, you can use the "whichdata" command to get it. If you mention the last character, the system program will not be executed correctly. $data='/usr/bin/data'; In the perl5 version, you can also use the following line of instructions to obtain the system time. $data=localtime(time);



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


Command:die
Syntax :dieLIST
Description: The LIST string will be displayed and the program will exit. Often used together with $!, which represents the error message variable.
Example: open(FILE,"$filename")||die"cannot open the file $!n; if the file fails to be opened, an error message will be displayed, and then the program will exit.



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


Command: open
Syntax 1: open(filehandle,"$filename")where $filename is a Specify the file name to open.
Description: This is a very commonly used function that can be used to open files (readonly). In CGI programming, a file is often opened to read data, so the author will explain the usage of this function in detail. This filehandle can be regarded as a bridge between I(INPUT)/O(OUTPUT), and FILEHANDLE can be used to read and write data. First, you can use the OPEN function to open a specified file. Next, you can use to read the data content of the opened file. Finally, you must use the close function to close the previously opened filehandle. It should be noted that in CGI program customization, when using the open function to open a file, the absolute path name of the file must be added before opening the file.
Example:
$filename="usr/abc.txt";
open(FILE,"$filename")||die"Cannot open file $filenamen;#Assign data to pure variable $line (one line Line by line)
while($line={
print "$line";
}
close(file); will display the contents of the file abc.txt

Syntax 2: open. (filehandle,"<$filename")
Explanation: This syntax can also open an existing file (readonly)
Example:
$filesname="usr/abc.txt";
open(file,"<$filename". )||die"Cannot open file $filenamen";
@array= to array@arrayclose(file);
print"@array"; will also The contents of the abc.TXT file are displayed.

Syntax 3: open(filehandle,">$filename")
Description: Create a new file (writeonly). If the file already exists, the old file name will be Overwrite it. And use printfilehandle to copy the data to the opened file.
Example:
$filename="/usr/abc.txt";
open(file,">$filename")||die" cannot be opened. File$filenamen;
printfile"thisisanewline1n;#n is the newline character
printfile"thisisanewline2n;
close(file); will store the data in a new file.

Syntax 4: open(filehandle,">>$filename")
Description: Data is appended to a file (writeonly). If the specified file name does not exist, a new file will be created.
Example:
$filename="/path/abc.txt";
open(file,">>$filename")||die "Cannot open file $filenamen";
printfile "thisisanewline1n";
printfile "thisisanewline2n" ;
close(file);
will append data to a file (abc.txt).

Syntax 5: open(filehandle,"|unixcommand")
Explanation: The data in filehandle will be input to the unix command for processing.
Example:
$mailprog="/usr/ucb/mail";#mail-sending program on unix system (must add absolute path)
$who="mqingyi@126.com";
$open(file," |$mailprog$who")||die"Failed to open n";
printfile"Iloveyou!n";
printfile"Iwanttoseeyou.n";
close(file);
will pass the unix system mail program to FILE The data content of this FILEHANDLE is sent to the recipient specified by the $who variable. We can use the open function to design a CGI application for writing letters, which will be introduced in detail in the next chapter of this book.



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


Command: close
Usage: close(filehandle)
Instructions: After using the open function to open a filehandle, you must use the close function to close the opened filehandle.
Example:
open(filehandle,"$filename");
close(filehandle);



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


Command: pack
Syntax: pack("specified format", list)
Description: The pack function will convert a list into the specified binary data format. In the CGI program segmentation and decoding process, the pack function will be used, so the author will briefly introduce the usage of this function here.
Example: $string=pack("c",65);#At this time, $string="a";converts the ASCII code 65 into an unsigned character, where c means to specify the character to be converted into an unsigned character.



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


Command: read
Syntax: read(filehandle,$string, length) where length represents the length (bytes) of the read string.
Description: Use the read function to read the data in filehandle according to the specified string length and assign it to the variable $string. During the cgi program segmentation and decoding process, if the FORM transmission method is set to POST, the transmitted data will be set to standard input, so the data content will be assigned to the filehandle of the standard input of STDIN, and the CGI environment variable $env{'content_length'} represents the length of the data content sent by the user, so we need to use the read function to obtain the data content sent by the user.
Example: read(stdin,$buffer,$env{'content_length'}); will read the data in the standard input filehandle of stdin according to the specified string length, and then assign it to the variable $buffer.



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


Command: exit
Syntax: exit
Description: Exit execution program.
Example: print "ilovecgin";exit; After displaying "ilovecgi", the program will exit.

The above is the content of Perl’s commonly used system functions. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template