无涯教程-Perl - my函数

65 阅读1分钟

描述

此函数声明LIST中的变量在包围式块内按词法范围。如果指定了多个变量,则所有变量都必须用括号括起来。

语法

以下是此函数的简单语法-

my LIST

返回值

此函数不返回任何值。

以下是显示其基本用法的示例代码-

#!/usr/bin/perl -w

my $string = "We are the world";
print "$string\n";
myfunction();
print "$string\n";

sub myfunction {
   my $string = "We are the function";
   print "$string\n";
   mysub();
}
sub mysub {
   print "$string\n";
}

执行上述代码后,将产生以下输出-

We are the world
We are the function
We are the world
We are the world

参考链接

www.learnfk.com/perl/perl-m…