What is the difference between my and local in Perl?

前端 未结 14 1648
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 03:13

I am seeing both of them used in this script I am trying to debug and the literature is just not clear. Can someone demystify this for me?

14条回答
  •  长情又很酷
    2020-12-01 03:52

    Look at the following code and its output to understand the difference.

    our $name = "Abhishek";
    
    sub sub1
    {
        print "\nName = $name\n";
        local $name = "Abhijeet";
    
        &sub2;
        &sub3;
    }
    
    sub sub2
    {
        print "\nName = $name\n";
    }
    
    sub sub3
    {
        my $name = "Abhinav";
        print "\nName = $name\n";
    }
    
    
    &sub1;
    

    Output is :

    Name = Abhishek
    
    Name = Abhijeet
    
    Name = Abhinav
    

提交回复
热议问题