MAO-EYE 2009-05-10 10:24 采纳率: 100%
浏览 424
已采纳

Perl 语言中 my 和 our 的区别是什么?

I know what my is in Perl. It defines a variable that exists only in the scope of the block in which it is defined. What does our do? How does our differ from my?

转载于:https://stackoverflow.com/questions/845060/what-is-the-difference-between-my-and-our-in-perl

  • 写回答

12条回答 默认 最新

  • 胖鸭 2009-05-20 02:22
    关注

    Great question: How does our differ from my and what does our do?

    In Summary:

    Available since Perl 5, my is a way to declare:

    • non-package variables, that are
    • private,
    • new,
    • non-global variables,
    • separate from any package. So that the variable cannot be accessed in the form of $package_name::variable.


    On the other hand, our variables are:

    • package variables, and thus automatically
    • global variables,
    • definitely not private,
    • nor are they necessarily new; and they
    • can be accessed outside the package (or lexical scope) with the qualified namespace, as $package_name::variable.


    Declaring a variable with our allows you to predeclare variables in order to use them under use strict without getting typo warnings or compile-time errors. Since Perl 5.6, it has replaced the obsolete use vars, which was only file-scoped, and not lexically scoped as is our.

    For example, the formal, qualified name for variable $x inside package main is $main::x. Declaring our $x allows you to use the bare $x variable without penalty (i.e., without a resulting error), in the scope of the declaration, when the script uses use strict or use strict "vars". The scope might be one, or two, or more packages, or one small block.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(11条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)