Perl Common system Functions List _ Basic Tutorial

Source: Internet
Author: User
Tags chop control characters pack rand regular expression stdin
list of common system functions

directive : Print
syntax : print FileHandle LIST
Description : This filehandle can be viewed as a bridge between I/O (OUTPUT), which can be used to make data read and write in the FileHandle. StdIn is the input from which the data is entered, for example, from the keyboard of the computer; StdOut is the output from which the data is being exported, for example, from the computer's screen; STDERR is the data from which the wrong output is being exported, for example, from the computer's screen. And in the Perl language there are three standard Filehandle:1.stdin (standard input): The FileHandle that represents STDIN
2.STDOUT (Standard output): Represents the FileHandle of STDOUT
3.STDERR (standard error Output): Is to represent STDERR filehandle if you want to use other filehandle, use the Open function to open a filehandle, We can output the data to FileHandle using the Print function list.
Before we introduce the print function, let's take a look at the special print characters in the print function:

directive:#
Description: annotation symbol Remark Declaration
Example : #这是一个注释说明

directive : Print
syntax : print FileHandle LIST
Description : This filehandle can be viewed as a bridge between I/O (OUTPUT), which can be used to make data read and write in the FileHandle. StdIn is the input from which the data is entered, for example, from the keyboard of the computer; StdOut is the output from which the data is being exported, for example, from the computer's screen; STDERR is the data from which the wrong output is being exported, for example, from the computer's screen. And in the Perl language there are three standard Filehandle:1.stdin (standard input): The FileHandle that represents STDIN
2.STDOUT (Standard output): Represents the FileHandle of STDOUT
3.STDERR (standard error Output): Is to represent STDERR filehandle if you want to use other filehandle, use the Open function to open a filehandle, We can output the data to FileHandle using the Print function list.
Before we introduce the print function, let's take a look at the special print characters in the print function:

Symbol of its role
\ n Wrap New Line
\ r Cursor Line change return
\ t Tab key
\f Change page Form feed
\b Back a grid.
\v The Vertical Tab key
\a Bell
\e Escape key
\007 decimal ASC II Code
\XFF Hexadecimal code
\c[control characters
example : Print STDOUT "online Learning park \ n"; The "Online learning garden" plus line-wrapping is displayed on the screen.

syntax : print LIST
Description : If omitted FileHandle, will filehandle the default for stdout. That is, the list's data content is displayed on the screen.
example : $url = "WWW.NETEASE.NET/~ZMD";
Print "Online learning garden $url\n";
On the screen there will be "online learning garden WWW.NETEASE.NET/~ZMD", if you want to make the variable in double quotes fail, you can precede the variable with the "\" symbol. For example: print "online learning garden \ $url"; So it shows: "Online learning garden $url"

Syntax : print
Note : With omitted filehandle and list words, will be stdout as filehandle, and will output $_ this output variable data content. If the $_ variable is an empty string, an empty string is displayed.
example : $_= "online learning park \ n"; Print The online learning garden will be added to the screen.

directive : printf
syntax : printf filehandle LIST
Description : The Perl language also references printf syntax in C, which is identical to the usage in C. If you omit FileHandle, The same will be stdout as a default filehandle. Before we introduce printf functions, let's take a look at the characters that transform the symbols in the printf function.

Symbol of its role
%c character
%s string
%d integer
%f Floating Integer
%H Hexadecimal code
%o Octal code
Example : printf ("chomod%d%s\n", "711" "CGI"); The chmod 711 CGI plus line wrap is displayed on the screen.

directive : Chop Syntax: Chop ($url)
Description : Delete 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 chop ($url = "www.nease.net/~zmd/");

directive : Split
syntax : Split (/pattern/, $text, limit) where/pattern/is the mode of word processing, and limit is to represent the number of partitions, generally can be omitted.
Description : Splits the $text string with a specified word processing pattern.
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");
When you transfer the data from the CGI application, you encode the data, separating the data contents of the First Data field in the form with the & symbol, so that each data field is separated by a character that is delimited by & when decoding. 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 are separated by = This symbol, if you want to get the name of the data field and the corresponding value, you use to = this symbol to split the data fields, such as: $name = "Mike=michael";
($name 1, $name 2) =split (/=/, $list); #这时 $name 1= "Mike" $name 2= "Michael";

directive : Keys
syntax : Keys (%array)
Note : Remove all key from the associative array%array.
Example :%name= (1, "Mike", 2, "Michael"); @readkey =keys (%names); #这时 @readkey = (1,2);

directive : Values
syntax : VALUES (%array)
Note : Remove all value in the associative array%array.
Example :%names= (1, "Mike", 2, "Michael"); @readval =values (%names); #这时 @readval = ("Mike", "Michael");

directive : Reverse
Syntax : reverse (@array)
description : Rearrange the elements in an array @array from back to front.
example : @back = ("A", "B", "C", "D", "E"); @back =reverse (@back); #这时 @back = ("E", "D", "C", "B", "A");

directive : Sort
syntax : sort (@array)
description : The elements in the array from small to large sort, if you want to order from large to small, add 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 as @abc= (reverse sort (@abc));
@number = (5,2,10); @number =sort (@number); The example above uses the sort function to order the values, so the following sentence should be used. @number = (sort{$a <=> $b} @number); #这时 @number = (2,5,10);

directive : Length
syntax : Length ($string)
Description : Find the Byte (bytes) value of the string $string.
example : $string = "PERL5"; $size =length ($string); #这时 $size = 5;

directive : substr
Syntax : substr ($string, offset,length) offset represents the position of the start character, length represents the string length of the reference, and if omitted length represents the last character length from the starting value to the string. If offset is negative, the character is specified 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";

directive : Index
Syntax : Index ($string, $substring, position) $substring is the character to look for; position representative from which position to start looking, if omitted position to start from scratch.
Description : Returns the position of the character to be searched in a string $string, and returns the value of 1 if no characters are found in the string.
Example :
$s =index ("perl5", "P"); #这时 $s =0
$s =index ("perl5", "L", 2); #这时 $s =3
$s =index ("perl5", "Perl"); #这时 $s =-1

directive : Push
syntax : Push (@array, $string)
Description : Appends the 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")

directive : Pop
syntax : Pop (@array)
description : Deletes 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: Appends a new element $string to the array @array before the first element in the array @array. example : @array = ("One", "two"); Unshift (@array, "three"); #这时 @array = ("Three", "one", "two")

directive : Shift
syntax : Shift (@array)
description : Deletes 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 : Adds a specified character $string between elements of an array @array and returns 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 the array elements of the text processing mode (regular expression).
Example :
@array = ("One", "on", "in");
$count =grep (/on/, @array); #这时 $count =2
@result =grep (/on/, @array); #这时 @result = ("One", "on");

directive : Hex
syntax : Hex ($string)
description : Converts hexadecimal values to decimal.
example : $decimal =hex ("FF"); At this time $decimal=255;

directive : Rand
Syntax : rand ($interger)
Note : Often and function srand collocation to obtain a random number, if the stand function is not first declared, then the normal value of the removed is a fixed. This syntax returns a value between 0 and $interger and, if $interger omitted, returns a value between 0 and 1.
Example :
Srand #要先宣告srand函数 in order to produce the effect of random numbers
$int =rand (10); # $int value is greater than 0 and less than 10 if you want to generate a number of random numbers, add int #这个函数
$int =int (rand (10)); # $int value is an integer, and the value is between 0 and 9.

directive : localtime
Syntax : localtime (Time)
Note : You can return nine elements of time, and use the time of the system when writing a CGI application, so this will detail the use of this function.
Example :
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $ISDST) =localtime (time);
Where: The $sec represents the number of seconds [0,59] $min represents the score [0,59] $hour represents the number of hours [0,23] $mday represents the number of months [1,31] $mon on behalf of the month [0,11], to be $mon plus 1, to meet the actual situation. $year the number of years since 1990 $wday from Saturday onwards, the representative is in the first days of the week [0-6] $yday from January 1, representing the day of the year [0,365] $ISDST just a flag know these variables, Can be applied in a CGI application. In addition, you can use the following line of instructions to get the system time under the UNIX system. 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 "which data" this command to learn. Finally, to mention the characters, you can not correctly execute the system's program. $data = '/usr/bin/data '; In the PERL5 version, you can also use the following line of instructions to get the system time. $data =localtime (time);

directive : Die
syntax : Die LIST
Description : Displays the list string and exits the program. This is often used in conjunction with $!, which represents error information variables.
Example : Open (FILE, "$filename") | | Die "Cannot open file $!\n; If you fail to open the file, you will be presented with the wrong information before exiting the program.

Directive : Open
Syntax 1 : Open (FileHandle, $filename) where $filename is a specified open file name.
Description : This is a very common function that can be used to open a file (read only). A file is often opened in the CGI program to read the data, so the author will elaborate on the relevant usage of this function. This filehandle can be regarded as a bridge between I (INPUT)/O (OUTPUT), which can be used to make the data read into the FileHandle. Start using the Open function to open a specified file, then you can use &ltfilehandle> to read the contents of the open file, and finally, you must use the close function to turn off the previously opened FileHandle. Note that in the CGI program, when you open a file using the Open function, be sure to add the file's absolute path name before opening the file.
Example :
$filename = "Usr/abc.txt";
Open (FILE, "$filename") | | Die "Cannot open file $filename\n; #将 &ltfile> data is assigned to a pure variable $line (one line)
while ($line =&ltfile>)
{
print $line;
}
Close (file), which displays the contents of the Abc.txt 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 =&ltfile> #将 &ltfile> All data contents are assigned to array @array close (file);
print "@array"; will also put ABC. TXT the contents of this file are displayed.

Syntax 3: Open (FileHandle, "> $filename")
Description : Create a new file (write only), if the file already exists, will overwrite the old file name. You can use the print FileHandle to bring the data to the open file.
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 newline character.
Print file "This is a new line2\n;
Close (file); There is a new file for the data to be typed.

Syntax 4: Open (FileHandle, ">> $filename")
Note : Data is attached to a file (write only), and a new file is created if the specified filename does not exist.
Example :
$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);
The data is attached (append) to a file (Abc.txt).

Syntax 5: Open (filehandle, "|unix command")
Description : will be processed in the FileHandle data input to UNIX instructions.
Example :
$mailprog = "/usr/ucb/mail"; #unix系统上的寄信程序 (be sure to add absolute path)
$who = "mqingyi@126.com";
$open (file, "| $mailprog $who") | | Die "Open failed \ n";
Print file "I love you!\n";
Print file "I want to you.\n";
Close (file);
The data content of file This filehandle is sent to the addressee specified by $who this variable through the UNIX system mail program. We can use the Open function to design a letter to criticize the CGI application, which will be described in detail in the next chapter of this book.

directive : Close
usage : Close (filehandle)
description : After opening a filehandle with open this function, be sure to use the close batch function to turn the open filehandle off.
Example :
Open (FileHandle, "$filename");
Close (FileHandle);

directive : Pack
Syntax : Pack ("specified format", list)
Description : Pack This function will be a list into the specified binary data format. In the CGI program partition decoding process, will use the pack this function, therefore the author briefly introduces this function the usage.
example : $string =pack ("C", 65); #这时 $string = "a"; converts the ASCII code of 65 into a unsigned character, where c specifies the meaning to be converted to unsigned characters.

directive : Read
syntax : Read (FileHandle, $string, length) where length is the length that represents the read string (bytes).
Note : Use the Read function to assign the data in the FileHandle to the $string variable after reading it in the specified string length. In the CGI program segmentation and decoding process, if the form is sent by the way is set to post, the transmitted data will be set to the standard input, so the data content will be assigned to stdin this standard input filehandle, and the CGI environment variable $env{' Content_ Length ' is the amount of data that the user sends out, so we'll use the Read function to get the data from the user.
Example : Read (stdin, $buffer, $env {' content_length '}); The data in stdin, the standard input filehandle, is read in the specified string length and then assigned to the $buffer variable.

directive : Exit
Syntax : Exit
description : Quits the execution of the program.
example : print "I love cgi\n"; Exit After displaying "I love CGI", you will quit this program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.