instruction:print
grammar:print Filehandle LIST
illustrate: This Filehandle can be regarded as a bridge between I(INPUT)/O(OUTPUT), and can use FILEHANDLE to make data reading and writing actions. STDIN represents where to input data, such as input from the computer's keyboard; STDOUT represents where to output data; eg output from the computer's screen; STDERR represents where to output wrong data, such as output from the computer's screen. There are three standard FILEHANDLE in PERL language: (standard input): is FILEHANDLE representing STDIN
(standard output): is the FILEHANDLE representing STDOUT
(Standard error output): It is 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 LIST data to output it to FILEHANDLE.
Before introducing the PRINT function to you, let’s take a look at the special printed characters in the PRINT function:
instruction:#
illustrate:Comment symbol Remark announcement
Example:#This is a comment
instruction:print
grammar:print Filehandle LIST
illustrate: This Filehandle can be regarded as a bridge between I(INPUT)/O(OUTPUT), and can use FILEHANDLE to make data reading and writing actions. STDIN represents where to input data, such as input from the computer's keyboard; STDOUT represents where to output data; eg output from the computer's screen; STDERR represents where to output wrong data, such as output from the computer's screen. There are three standard FILEHANDLE in PERL language: (standard input): is FILEHANDLE representing STDIN
(standard output): is the FILEHANDLE representing STDOUT
(Standard error output): It is 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 LIST data to output it to FILEHANDLE.
Before introducing the PRINT function to you, let’s take a look at the special printed characters in the PRINT function:
Symbols What it does
\n New line
\r Cursor line break return
\t tab key
\f Change form feed
\b Return to one box
\v Vertical tab key
\a Bell
\e escape key
\007 Decimal ASC II code
\xff Hexadecimal code
\c[ Control character
Example: print STDOUT "Online School\n"; Display "Online School" plus line breaks on the screen.
grammar: print LIST
illustrate: If Filehandle is omitted, the Filehandle will be set as STDOUT. That is, the LIST data content will be displayed on the screen.
Example: $url="/~zmd";
print "Online school$url\n";
"Online School/~zmd" will appear on the screen. If you want to invalidate the variables in double quotes, you can add the "\" symbol before the variable. For example: print "Online School\$url"; so it will display: "Online School$url"
grammar: print
illustrate: If Filehandle and LIST are omitted, STDOUT will be used as Filehandle, and the data content of the internal output variable $_ will be output. If the $_ variable is an empty string, an empty string will be displayed.
Example: $_="Online School\n"; print; will display "Online School" plus line breaks on the screen
instruction: printf
grammar: printf Filehandle LIST
illustrate: The syntax of printf in C language is also upgraded in perl language, and the usage is exactly the same as that in C language. If Filehandle is omitted, STDOUT will also be regarded as a built-in Filehandle. Before introducing the printf function to you, let's take a look at the characters in the printf function for transforming symbols.
Symbols What it does
%c Character
%s String
%d integer
%f Floating integer
%h Hexadecimal code
%o Octal code
Example: printf("chomod%d%s\n","711""cgi"); will display chmod 711 cgi plus line breaks on the screen.
instruction:chop Syntax:chop($url)
illustrate: Delete the last character.
Example:$url="/~zmd/";
chop($url); At this time, $url="/~zmd" and these two lines can also be written as chop($url="/~zmd/");
instruction:split
grammar: split(/pattern/,$text,limit) where /pattern/ is the word processing pattern, and limit represents the number to be divided, which can generally be omitted.
illustrate: 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, which will separate the data content of the first data field in the FORM with the & symbol. Therefore, when decoding, each data field must be divided with the & symbol as the character divided. For example: $text="Mike=A&Michael=B";
@name=split(/&/,$text); #At this time, @name=("Mike=A","Michael=B"); The name of the data field is separated from the value of the data field by the = symbol. If you want to obtain the name of the data field and the corresponding value, you should use the = symbol to divide the data field, for example: $name=""Mike=Michael"";
($name1,$name2)=split(/=/,$list); #At this time $name1="Mike";$name2="Michael";
instruction:keys
grammar:keys(%array)
illustrate: Take out all keys in the associative array %ARRAY.
Example: %NAME=(1,"mike",2,"michael"); @readkey=keys(%NAMES); #At this time @readkey=(1,2);
instruction:values
grammar:values(%array)
illustrate: Take out all values in the associative array %ARRAY.
Example: %NAMES=(1,"mike",2,"michael"); @readval=values(%NAMES); #At this time @readval=("mike","michael");
instruction:reverse
grammar:reverse(@array)
illustrate: Rearrange the elements in the array @array from back to front.
Example:@back=("A","B","C","D","E"); @back=reverse(@back); #At this time @back=("E","D","C","B","A");
instruction:sort
grammar:sort(@array)
illustrate: Sort the elements in the array from small to large. If you want to sort from large to small, add the reverse function.
Example:
@abc=("d","b","c","a"); @abc=sort(@abc); #At this time @abc=("a","b","c","d");
@abc=(reverse sort@abc); #At this time @abc=("d","c","b","a"); This syntax can also be written as @abc=(reverse sort(@abc));
@number=(5,2,10); @number=sort(@number); The above example uses the sort function to sort values, and there will be an error, so you need to use the following sentence to the following sentence. @number=(sort{$a<=>$b}@number); #At this time @number=(2,5,10);
instruction:length
grammar:length($string)
illustrate: Find the bytes value of the string $string.
Example:$string="Perl5"; $size=length($string); #At this time $size=5;
instruction:substr
grammar: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 represents the length of the last character from the starting value to the string. If offset is a negative value, the character will be specified 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";
instruction:index
grammar: index($string,$substring,position) $substring is the character to be searched for; position represents which position to start looking for. If position is omitted, start from scratch.
illustrate: Returns the position of the character you are looking for in a string $string. If no characters are 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
instruction:push
grammar:push(@array,$string)
illustrate: Attach new element ($string) to the array @array at the end of the array @array.
Example:@array=("one","two"); push(@array,"three"); #At this time $@array=("one","two","three")
instruction:pop
grammar:pop(@array)
illustrate: Delete the last element of the array (@array) and return the deleted element.
Example:@array=("one","two"); $rm=pop(@array); #At this time @array=("one"); and $rm="two";
instruction:unshift
grammar:unshift(@array,$string) Description: Append a 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")
instruction:shift
grammar:shift(@array)
illustrate: Delete the first element of the array @array and return the deleted element.
Example:@array=("one","two"); @rm=shift(@array); #At this time @array=("two"); and $rm="one";
instruction:join
grammar:join($string,@array)
illustrate: Add a specified character $string between elements of an array @array and return the result.
Example:
@array=("one","two","three");
$total=join(":",@array); At this time, $total="one: two: three";
instruction:grep
grammar:grep(/pattern/,@array)
illustrate: Find out the array elements of the 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");
instruction:hex
grammar:hex($string)
illustrate: Convert hexadecimal values to decimal.
Example: $decimal=hex("ff"); At this time $decimal=255;
instruction:rand
grammar:rand($interger)
illustrate: Usually paired with the function srand to obtain a random number. If the stand function is not declared first, the constant value taken is a fixed value. This syntax returns a value between 0 and $interger, and if $interger is omitted, it returns a value between 0 and 1.
Example:
srand; #You must declare the srand function 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 the random number you want to generate is an integer, add the function int #
$int=int(rand(10)); #The value of $int is an integer and the value is between 0 and 9
instruction:localtime
grammar:localtime(time)
illustrate: It can return nine time-related elements. System time is often used when writing CGI applications, so the usage of this function will be introduced in detail here.
Example:
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
Among them: $sec represents the number of seconds [0,59] $min represents the number of fractions [0,59] $hour represents the number of hours [0,23] $mday represents the day of this month [1,31] $mon represents the number of months [0,11]. You must add $mon 1 to meet the actual situation. $year counts from 1990 $wday counts from Saturday, which represents the day of the week [0-6] $yday counts from January 1, which represents the day of the year [0,365] $isdst is just a flag After knowing these variables, it can be applied in a CGI application. In addition, you can also use the following line of instructions to obtain 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" command to know. If you want to mention characters in the end, you cannot execute the system program correctly. $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);
instruction:die
grammar:die LIST
illustrate: It will display the LIST string and exit the program. Often used with $! This represents error message variable.
Example:open(FILE,"$filename")||die "Cannot open file $!\n; If the file is opened, an error message will be displayed, and then exit the program.
instruction:open
Syntax 1:open(filehandle,"$filename") where $filename is a specified open file name.
illustrate: This is a very commonly used function that can be used to open a file (read only). In CGI programming, a file is often opened to read data, so the author will explain in detail the usage of this function. This filehandle can be regarded as a bridge between I(INPUT)/O(OUTPUT), and can use FILEHANDLE to perform data reading and writing operations. Start by using the OPEN function to open a specified file. Next, you can use <filehandle> to read the data content of the opened file. Finally, you must use the close function to close the previously opened file. It should be noted that in CGI program customization, when using the OPEN function to open a file, you must add the absolute path name of the file before opening the file.
Example:
$filename="usr/";
open(FILE,"$filename")||die" cannot open the file $filename\n; # Assign <file> data to the pure variable $line (one line by line)
while($line=<file>)
{
print"$line";
}
close(file); will display the contents of this file.
Syntax 2:open(filehandle,"<$filename")
illustrate: This syntax can also open an existing file (read only).
Example:
$filesname="usr/";
open(file,"<$filename")||die"Cannot open file $filename\n";
@array=<file> #Assign all the data content of<file> to the array @array close(file);
print "@array"; will also display the contents of this file.
Syntax 3:open(filehandle,">$filename")
illustrate: Create a new file (write only). If this file already exists, the old file name will be overwritten. And you can use the print filehandle method to transfer the data into the opened file.
Example:
$filename="/usr/";
open(file,">$filename")||die" cannot open file $filename\n;
print file "this is a new line1\n; #\n is a new line character
print file "this is a new line2\n;
close(file); will enter data and exist in a new file.
Syntax 4:open(filehandle,">>$filename")
illustrate: The data is added to a file (write only). If the specified file name does not exist, a new file will be created.
Example:
$filename="/path/";
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);
Data will be appended to a file().
Syntax 5:open(filehandle,"|unix command")
illustrate: The data in filehandle will be input to the unix instruction for processing.
Example:
$mailprog="/usr/ucb/mail"; #unix system sending program (must add an absolute path)
$who="mqingyi@";
$open(file,"|$mailprog$who")||die "Open failed\n";
print file "I love you!\n";
print file "I want to see you.\n";
close(file);
The FILEHANDLE data content will be sent to the recipient specified by the $who variable through the unix system mail program. We can use the function open to design a CGI application that criticizes letters, which will be introduced in detail in the next chapter of this book.
instruction:close
usage:close(filehandle)
illustrate: After using the open function to open a filehandle, be sure to use the close batch function to close the opened filehandle.
Example:
open(filehandle,"$filename");
close(filehandle);
instruction:pack
grammar: pack("Specified format", list)
illustrate:pack This function will turn a list into the specified binary data format. In the process of segmentation and decoding of CGI programs, the function pack will be used, so the author briefly introduces 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 specifies the meaning of converting it into an unsigned character.
instruction:read
grammar: read(filehandle,$string,length) where length represents the length of the string (bytes).
illustrate: Use the read function to read the data in the filehandle according to the specified string length and assign it to the $string variable. During the cgi program segmentation and decoding process, if the FORM transmission method is set to POST, the transmitted data will be set as 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. Therefore, we need to use the read function to obtain the data content sent by the user.
Example: read(stdin,$buffer,$env{'content_length'}); The data in the standard input filehandle of stdin will be read in accordance with the specified string length, and then assigned to the $buffer variable.
instruction:exit
grammar:exit
illustrate: Exit the executed program.
Example: print"i love cgi\n"; exit; After displaying "i love cgi", the program will be exited.