What is the difference between my and local in Perl?

前端 未结 14 1608
佛祖请我去吃肉
佛祖请我去吃肉 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:49

    &s;
    
    sub s()
    {
        local $s="5";
        &b;
        print $s;
    }
    
    sub b()
    {
        $s++;
    }
    

    The above script prints 6.

    But if we change local to my it will print 5.

    This is the difference. Simple.

提交回复
热议问题