How to define and call functions in Perl?

In Perl, you can define functions using the “sub” keyword. Here is an example of defining and calling a function:

# 定义函数
sub hello {
    my $name = shift;
    print "Hello, $name!\n";
}

# 调用函数
hello("Alice");
hello("Bob");

In the example above, a function named hello is defined, which takes a parameter $name and prints a simple greeting. Then, the function is called by passing different parameters to it.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds