珀尔 |我的关键词
Perl 中的 my 关键字声明列出的变量对于定义它的封闭块是本地的。 my 的目的是定义静态范围。这可用于多次使用相同的变量名但具有不同的值。注意:要在 my 关键字下指定多个变量,请使用括号。
Syntax: my variable
Parameter:
variable: to be defined as local
Returns:
does not return any value.
示例 1:
#!/usr/bin/perl -w
# Local variable outside of subroutine
my $string = "Geeks for Geeks";
print "$string\n";
# Subroutine call
my_func();
print "$string\n";
# defining subroutine
sub my_func
{
# Local variable inside the subroutine
my $string = "This is in Function";
print "$string\n";
mysub();
}
# defining subroutine to show
# the local effect of my keyword
sub mysub
{
print "$string\n";
}
输出:
Geeks for Geeks
This is in Function
Geeks for Geeks
Geeks for Geeks
示例 2:
#!/usr/bin/perl -w
# Local variable outside of subroutine
my $string = "Welcome to Geeks";
print "$string\n";
# Subroutine call
my_func();
print "$string\n";
# defining subroutine
sub my_func
{
# Local variable inside the subroutine
my $string = "Let's GO Geeky!!!";
print "$string\n";
mysub();
}
# defining subroutine to show
# the local effect of my keyword
sub mysub
{
print "$string\n";
}
输出:
Welcome to Geeks
Let's GO Geeky!!!
Welcome to Geeks
Welcome to Geeks
如何定义动态范围?
“我的”的反义词是“本地的”。 local 关键字定义动态范围。
# A perl code to demonstrate dynamic scoping
$x = 10;
sub f
{
return $x;
}
sub g
{
# Since local is used, x uses
# dynamic scoping.
local $x = 20;
return f();
}
print g()."\n";