Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Perl instructions

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "what are the Perl instructions", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "what are the Perl instructions?"

Perl instruction Collection

Perl instruction: print

Syntax: printFilehandleLIST

Note: this Filehandle can be seen as a bridge between I (INPUT) / O (OUTPUT). FILEHANDLE can be used to read and write data. STDIN represents where to input data, such as the keyboard of a computer; STDOUT represents where to output data; for example, output from a computer screen; STDERR represents where to output incorrect data, such as output from a computer screen. In Perl, there are three standard FILEHANDLE:1.STDIN (standard input): FILEHANDLE, which represents STDIN.

2.STDOUT (standard output): is the FILEHANDLE that represents STDOUT

3.STDERR (standard error output): FILEHANDLE represents STDERR. If you want to use other FILEHANDLE, you should use the function OPEN to open a FILEHANDLE. We can use the function PRINT to output the data of LIST to FILEHANDLE.

Before we introduce the PRINT function, let's take a look at the special printed characters in the PRINT function:

Perl directive: #

Description: annotation symbol Remark declaration

Example: # this is a note

Perl instruction: print

Syntax: printFilehandleLIST

Note: this Filehandle can be seen as a bridge between I (INPUT) / O (OUTPUT). FILEHANDLE can be used to read and write data. STDIN represents where to input data, such as the keyboard of a computer; STDOUT represents where to output data; for example, output from a computer screen; STDERR represents where to output incorrect data, such as output from a computer screen. In Perl, there are three standard FILEHANDLE:1.STDIN (standard input): the FILEHANDLE that represents STDIN.

2.STDOUT (standard output): is the FILEHANDLE that represents STDOUT

3.STDERR (standard error output): FILEHANDLE represents STDERR. If you want to use other FILEHANDLE, you should use the function OPEN to open a FILEHANDLE. We can use the function PRINT to output the data of LIST to FILEHANDLE.

Before we introduce the PRINT function, let's take a look at the special printed characters in the PRINT function:

The function of symbols

Line break newline

Cursor wrapping return

Tab key

F page change formfeed

Return one frame

V vertical tab key

A ring Bell

Eescape key

07 decimal ASCII code

Xff hexadecimal code

C [control character

Example: printSTDOUT "online campus"; "online campus" plus line feeds are displayed on the screen.

Syntax: printLIST

Note: if Filehandle is omitted, Filehandle will be defined as STDOUT. That is, the data content of LIST will be displayed on the screen.

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

Print "online Campus $url"

The "online campus www.netease.net/~zmd" will appear on the screen. If you want to invalidate the variable in double quotation marks, you can add a "" symbol before the variable. For example: print "online campus $url"; so it shows "online campus $url"

Syntax: print

Note: if Filehandle and LIST are omitted, STDOUT will be used as the Filehandle, and the data contents of the default output variable $_ will be output. If the $_ variable is an empty string, an empty string is displayed.

Example: $_ = "online campus"; print; will display the "online campus" plus line breaks on the screen

Perl instruction: printf

Syntax: printfFilehandleLIST

Description: the syntax and usage of printf in Perl language are exactly the same as those in C language. If you omit Filehandle, you will also regard STDOUT as a built-in Filehandle. Before we introduce the printf function, let's take a look at the characters that transform symbols in the printf function.

The function of symbols

% c character

% s string

% d integer

% f floating integer

% h hexadecimal code

% o octal code

Example: printf ("chomod%d%s", "711"CGI"); chmod711cgi is displayed on the screen with line breaks.

Perl instruction: chop

Syntax: chop ($url)

Description: delete a character.

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

Chop ($url); at this point $url= "www.nease.net/~zmd" and these two lines can also be written as chop ($url= "www.nease.net/~zmd/")

Perl instruction: split

Syntax: split (/ pattern/,$text,limit) where / pattern/ is the mode of word processing, and limit represents the number to be divided, which can be omitted.

Description: splits the $text string with a specified word processing mode.

Example:

$text= "Michael,Gevin,Mike"; @ name=split (/, /, $text); # this time @ name= ("Michael", "Gevin", "Mike")

($a Michael (/, /, $text); # then $a = "Michael"; $b = "Gevin"; $c = "Mike"

@ name=split (/, /, $string,2); # this time @ name= ("Michael", "Gevin")

When transmitting CGI application data, the data is encoded first, and the data content of the first data field in the FORM is separated by the & symbol, so each data field is separated by the & symbol when decoding. For example: $text= "Mike=A&Michael=B"

@ name=split (/ & /, $text); # then @ name= ("Mike=A", "Michael=B"); and the name of the data field and the value of the data field are separated by the = symbol. If you want to get the name and the corresponding value of the data field, use the = symbol to separate the data field, for example: $name= "" Mike=Michael ".

($name1,$name2) = split (/ = /, $list); # now $name1= "Mike"; $name2= "Michael"

Perl instruction: keys

Syntax: keys (% array)

Description: fetch all the key in the associative array% ARRAY.

Example:% NAME= (1, "mike", 2, "michael"); @ readkey=keys (% NAMES); # at this time @ readkey= (1Mague 2)

Perl instruction: values

Syntax: values (% array)

Description: fetch all the value in the associative array% ARRAY.

Example:% NAMES= (1, "mike", 2, "michael"); @ readval=values (% NAMES); # at this time @ readval= ("mike", "michael")

Perl instruction: reverse

Syntax: reverse (@ array)

Description: rearrange the elements in the array @ array from back to front.

Example: @ back= ("A", "B", "C", "D", "E"); @ back=reverse (@ back); # then @ back= ("E", "D", "C", "B", "A")

Perl instruction: sort

Syntax: sort (@ array)

Description: sort the elements in the array from small to large. If you want to sort from large to small, add the function reverse.

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= (5Jing 2jue 10); @ number=sort (@ number); in the above example, there is an error when you use the sort function to sort values, so use the following sentence. @ number= (sort {$aqb} @ number); # at this time @ number= (2pm 5pm 10)

Perl instruction: length

Syntax: length ($string)

Description: find the bytes value of the string $string.

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

Perl instruction: substr

Syntax: substr ($string,offset,length) offset represents the position of the starting character, length represents the length of the referenced string, and omitting length represents the length of one character from the starting value to the string. If offset is negative, the character is specified starting at the right side of the string.

Example:

$s=substr ("perl5", 2jue 2); # at this time $s = "rl"

$s=substr ("perl5", 2); # at this time $s = "rl5"

$s=substr ("perl5",-2jue 2); # then $s = "er"

Perl instruction: index

Syntax: index ($string,$substring,position) $substring is the character you are looking for; position represents where to start the search, and if you omit position, start from scratch.

Description: returns the position of the character you are looking for in a string $string, and returns the value of-1 if the character cannot be found in the string.

Example:

$s=index ("perl5", "p"); # at this time $sound0

$s=index ("perl5", "l", 2); # at this time $slots 3

$s=index ("perl5", "perl"); # at this time $splashly1

Perl instruction: push

Syntax: push (@ array,$string)

Description: append a new element ($string) to the array @ array in the array @ array.

Example: @ array= ("one", "two"); push (@ array, "three"); # then $@ array= ("one", "two", "three")

Perl instruction: pop

Syntax: pop (@ array)

Description: delete an element of the array (@ array) and return the deleted element.

Example: @ array= ("one", "two"); $rm=pop (@ array); # this time @ array= ("one"); and $rm= "two"

Perl instruction: unshift

Syntax: unshift (@ array,$string) description: append a new element $string to the array @ array before the * elements of the array @ array. Example: @ array= ("one", "two"); unshift (@ array, "three"); # at this time @ array= ("three", "one", "two")

Perl instruction: shift

Syntax: shift (@ array)

Description: delete the * * elements of the array @ array and return the deleted elements.

Example: @ array= ("one", "two"); @ rm=shift (@ array); # this time @ array= ("two"); and $rm= "one"

Perl instruction: join

Syntax: join ($string,@array)

Description: add a specified character $string between the elements of an array @ array and return the result.

Example:

@ array= ("one", "two", "three")

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

Perl instruction: grep

Syntax: grep (/ pattern/,@array)

Description: find the array elements that match the word processing mode (regularexpression).

Example:

@ array= ("one", "on", "in")

$count=grep (/ on/,@array); # now $count=2

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

Perl instruction: hex

Syntax: hex ($string)

Description: convert hexadecimal values to decimal.

Example: $decimal=hex ("ff"); now $decimal=255

Perl instruction: rand

Syntax: rand ($interger)

Description: often collocated with the function srand to get a random number, if the stand function is not declared first, the constant value taken out is a fixed value. This grammar returns a value between 0 and $interger, or between 0 and 1 if $interger is omitted.

Example:

Srand;# must declare the srand function before it can 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#.

The value of $int=int (rand (10)); # $int is an integer and is between 0 and 9

Perl instruction: localtime

Syntax: localtime (time)

Description: nine time-related elements can be returned, and the system time is often used when writing CGI applications, so the usage of this function will be described in detail here.

Example:

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime (time)

Among them: $sec represents seconds [0mday 59] $min represents score [0jue 59] $hour represents hours [0jue 23] $mday represents the number of months [0jing31] $mon represents number of months [0Power11], it is necessary to add $mon to 1 before it can accord with the actual situation. The number of years of $year from 1990$ wday from Saturday, the day of the week [0-6] $yday from January 1, the day of the year [0365] $isdst is just a flag that can be used in CGI applications after knowing these variables. In addition, you can also use the following line of instructions to get the system time under the UNIX system. To avoid errors, * uses the absolute path to obtain the system time. If the absolute path is not clear, you can use the "whichdata" instruction to know. If you want to pick up characters, you will not be able to execute the system program correctly. $data='/usr/bin/data'; and in the perl5 version, you can also use the following line of instructions to get the system time. $data=localtime (time)

Perl instruction: die

Syntax: dieLIST

Description: the LIST string is displayed and the program is exited. Often with $! This represents error message variables to be used together.

Example: open (FILE, "$filename") | | die "cannot open the file $!; if it fails to open the file, it will display an error message and then exit the program.

The above is all the contents of this article "what are the Perl instructions?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report