衫裤跑路 2008-12-24 19:48 采纳率: 50%
浏览 464
已采纳

Perl 的"bless"到底是做什么的?

I understand one uses the "bless" keyword in Perl inside a class's "new" method:

sub new {
    my $self = bless { };
    return $self;
}    

But what exactly is "bless" doing to that hash reference ?

转载于:https://stackoverflow.com/questions/392135/what-exactly-does-perls-bless-do

  • 写回答

7条回答 默认 最新

  • derek5. 2008-12-24 19:58
    关注

    In general, bless associates an object with a class.

    package MyClass;
    my $object = { };
    bless $object, "MyClass";
    

    Now when you invoke a method on $object, Perl know which package to search for the method.

    If the second argument is omitted, as in your example, the current package/class is used.

    For the sake of clarity, your example might be written as follows:

    sub new { 
      my $class = shift; 
      my $self = { }; 
      bless $self, $class; 
    } 
    

    EDIT: See kixx's good answer for a little more detail.

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

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?