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

How to use Perl user function

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use the Perl user function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Perl user function

Perl user functions, also known as subroutines (Subroutine), define Perl user functions in Perl with the following structure:

Subroutine name {statement block;}

The subroutine name here is similar to the variable naming rule.

Take the program that displays welcome words as an example:

Subsay_hello {print "Hello, welcome to the online learning park";}

The definition of the Perl user function can be anywhere in the program, such as at the end of the file. If two subroutines use the same program name, the following subroutines will overwrite the previous subroutines.

Variables in Perl user functions default to global variables and are shared with other programs.

Perl user function call: it can be called in any expression by adding a "&" call to the subroutine. Another subroutine can be called in the subroutine.

The result of calling the Perl user function is called the returnvalue. The return value is the calculated value of an expression in each call to the function. Take the addition function as an example:

Subadd_a_b {$astatb;}

The function * * has an expression of $axifolb, so the return value is $axifolb. The following is the call:

$axi5

$baked 6

The value of $cantilever addenda to bincture roomc is 11.

The value of $dici5 is 5: 11 or 55.

The functions of the above Perl user functions are no different from those written directly in the program. If you add the parameter transfer, you can achieve a new function. In Perl, if a function call is followed by a list enclosed in parentheses, the list is automatically assigned to a special variable named @ _ during the function call. The function can access the variable to determine the number of parameters and their assignments.

Take the addition function as an example:

Subadd_a_b {$_ [0] + $_ [1];} $c=&add_a_b (5 d=5*&add_a_b 6); # $c has a value of 11$ d=5*&add_a_b (2); and # d has a value of 5 to 5 or 25

How to change the number of parameters? We can do this in a circular way:

Subadd_all {$sum=0;# initializes sum foreach$_ (@ _) {# traversing the parameter list $sum+=$_;# accumulates each element} $sum;# returns the value of sum that is the sum} $a=&add_all (3jin4); # $a has a value of 3, 4, 5, that is, 12$ d=2*&add_all (1), and # d has a value of 2, 15, or 30

Since the variables in the Perl user function are all full variables, it will be replaced if the caller contains the $sum variable in the above program, which is not what we want. So how to solve this problem?

The answer is: use local variables and use the local () operator to do this. In the above program, simply add the following before the * line $sum=0;:

Local ($sum)

When the function executes, the value of the entire variable of $sum is retained, and a local variable $sum is created, and the value of the full variable $sum is restored after exiting the function add_all. Such as:

Subadd_all {local ($sum); # defines $sum as a local variable $sum=0;# initializes sum foreach$_ (@ _) {# traversing the parameter list $sum+=$_;# accumulates each element} $sum;# returns the value of sum that is the sum} the original value of $sum=88;#$sum is 88 print$sum;# shows that the value of $sum is 88$ a=&add_all, that is, 12 print$sum;# shows that the value of $sum is still 88

Comparison: if the local ($sum); row is not added, the execution result of the * * row will be 12.

Here is another example: calculate the addition within ten and output it in Chinese. When the Arabic numerals are output after 10:00. For example, enter two numbers to show "one plus two equals three". The procedure is as follows:

# /! / usr/bin/perl subcnumber {@ chinese= ("0", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"); # define @ chinese as an array local ($number); # define number as a local variable $chinese ($number) | | number;# think why? } # main program print "Please enter a number:" chop ($num1=); print "Please enter another number:" chop ($num2=); $msg=&chinese ($num1). "add". & chinese ($num2). Equal to. & chinese ($num1+num2). ".\ n" print$msg;# prints the value of $msg where $chinese ($number) | | the number; line is written as follows: if ($chinese (number) {& chinese ($number); # returns Chinese} else {& number;# returns Arabic numerals)

Take a look at the execution result: enter 2Pol 3, which shows: "two plus three equals five." Enter 3 focus 12, which displays: "three plus 12 equals 15."

Thank you for reading this article carefully. I hope the article "how to use Perl user functions" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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