📜  珀尔 |变量范围

📅  最后修改于: 2022-05-13 01:54:39.800000             🧑  作者: Mango

珀尔 |变量范围

变量的范围是程序中可以访问变量的部分。范围也称为程序中变量的可见性。在 Perl 中,我们可以声明全局变量私有变量。私有变量也称为词法变量

全局变量的范围

全局变量可以在程序中创建的任何函数或任何块中使用。它在整个程序中可见。全局变量可以直接使用并且可以从程序的每个部分访问。

示例 1:变量$name在代码开头声明。它在任何地方都可见,直到文件末尾。甚至在街区内。即使那些在函数声明中。如果我们更改块内的变量,那将更改其余代码的值。甚至在街区之外。

# Perl program to illustrate the 
# Scope of Global variables
  
# declaration of global variable 
$name = "GFG";
  
# printing global variable 
print "$name\n";    
  
# global variable can be used 
# inside a block, hence the we 
# are taking a block in which
# we will print the value of 
# $name i.e. global variable
{
  
    # here GFG will print
    print "$name\n"; 
      
    # values in global variable can be
    # changed even within a block, 
    # hence the value of $name is 
    # now changed to "GeeksforGeeks"
    $name = "GeeksforGeeks"; 
      
    # print function prints
    # "GeeksforGeeks"
    print "$name\n"; 
}
  
# changes made inside the above block'
# are reflected in the whole program 
# so here GeeksforGeeks will print
print "$name\n"; 
输出:
GFG
GFG
GeeksforGeeks
GeeksforGeeks

示例 2:

# Perl program to illustrate the 
# Scope of Global variables
  
# declaration of global variables
$name = "GFG"; 
$count = 1;
  
# printing global variables
print $count." ".$name."\n";
$count++;
  
# Block starting
{
      
    # global variable can be used inside
    # a block, so below statement will 
    # print GFG and 1
    print $count." ".$name."\n";
      
    # incrementing the value of 
    # count inside the block
    $count++;
}
  
# taking a function
sub func {
      
    # Global variable, $count and $name,
    # are accessible within function
    print $count." ".$name."\n";
}
  
# calling the function
func();
输出:
1 GFG
2 GFG
3 GFG

词法变量的范围(私有变量)

Perl 中的私有变量是在变量之前使用my关键字定义的。 my关键字将变量限制在声明它的函数或块中。块可以是 for 循环、while 循环或带有花括号的代码块。局部变量的作用域是局部的,它的存在位于这两个花括号(代码块)之间,在那个块之外这个变量不存在。这些变量也称为词法变量。

注意:在函数或块中使用私有变量时,它们会隐藏使用相同名称创建的全局变量。当我们调用带有私有变量的子例程时,它可以在该函数中使用。一旦子例程退出,就不能再使用私有变量。

例子:

# Perl program to illustrate the 
# scope of private variables
  
# declaration of global variable 
$name = "Global"; 
$count = 1;
  
# printing global variables
print $count." ".$name."\n";
  
# incrementing the value of count
# i.e it become 2
$count++;
  
# block starting
{
      
    # declaring private variable by using my 
    # keyword which can only be used
    # within this block
    my $new_name = "Private"; 
      
    # global variables are 
    # accessible inside block
    print $count." ".$name."\n";
      
    # incrementing the value
    # of global variable
    # here it become 3
    $count++;
      
    print $name." and ".$new_name."\n";
}
  
# $new_name variable cannot 
# be used outside, hence nothing 
# is going to print 
print "Variable defined in above block: ".$new_name."\n"; 
  
# declaring function
sub func {
  
    # this private variable declaration
    # hides the global variable which define
    # in the beginning of program
    my $name = "Hide"; 
    print $count." ".$name."\n";
  
}
  
# calling the function
func();
输出:
1 Global
2 Global
Global and Private
Variable defined in above block: 
3 Hide

包变量

在 Perl 中,我们还有另一种类型的作用域,称为包作用域。当我们需要创建可以在不同命名空间中独占使用的变量时,就会使用它。” main ”是每个 Perl 程序中的默认命名空间。 Perl 中的命名空间是使用package关键字定义的。

例子:

# Perl program to illustrate 
# the Package Variables
  
# variable declared in 
# main namespace
$var1 = "Main Namespace";
  
print "Value of Var1: ".$var1."\n";
   
# package declaration
# Pack1 is the package 
package Pack1;
   
    # since $var1 belongs to main namespace, 
    # so nothing will print inside Pack1
    # namespace
    print "Value of var1: ".$var1."\n"; 
       
    # variable declared in Pack1 namespace
    # having same name as main namespace
    $var1 = "Pack1 Namespace";
      
    # here $var1 belongs to Pack1 namespace
    print "Value of var1: ".$var1."\n";
       
    # in-order to print variables 
    # from both namespace, use 
    # following method
    print "Value of var1: ".$main::var1."\n";
    print "Value of var1: ".$Pack1::var1."\n";
输出:
Value of Var1: Main Namespace
Value of var1: 
Value of var1: Pack1 Namespace
Value of var1: Main Namespace
Value of var1: Pack1 Namespace

我们在 Perl 中的关键字: “our”关键字只为现有的同名包变量创建别名。 our关键字允许使用包变量而不用包名限定它,但只能在“ our ”声明的词法范围内。使用我们的关键字声明的变量为包变量声明了一个别名,该别名将在其整个词法范围内可见,甚至跨越包边界

# Perl program to illustrate the use 
# of our keyword
  
# Pack1 namespace declared
# by using the package keyword
package Pack1;
   
    # declaring $Pack1::first_name 
    # for rest of lexical scope
    our $first_name;    
    $first_name = "Shashank";
       
    # declaring $Pack1::second_name for
    # only this namespace
    $second_name;    
    $second_name = "Sharma";
   
# Pack2 namespace declared
package Pack2;
   
    # prints value of $first_name, as it 
    # refers to $Pack1::first_name
    print "first_name = ".$first_name."\n";
       
    # It will print nothing as $second_name
    # does not exist in Pack2 package scope
    print "second_name = ".$second_name."\n";  
输出:
first_name = Shashank
second_name =