152

我知道在类的“new”方法中使用 Perl 中的“bless”关键字:

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

但是“祝福”对那个哈希引用到底做了什么?

4

8 回答 8

152

通常,bless将对象与类相关联。

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

现在,当您在 上调用方法时$object,Perl 知道要在哪个包中搜索该方法。

如果第二个参数被省略,如您的示例所示,则使用当前包/类。

为了清楚起见,您的示例可能会编写如下:

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

编辑:有关更多详细信息,请参阅kixx的好答案

于 2008-12-24T19:58:09.900 回答
84

bless将引用与包相关联。

引用是什么并不重要,它可以是哈希(最常见的情况)、数组(不太常见)、标量(通常这表示一个由内而外的对象)、正则表达式,子例程或 TYPEGLOB(有关有用的示例,请参阅Damian Conway 的《面向对象的 Perl:概念和编程技术综合指南》一书),甚至是对文件或目录句柄的引用(最不常见的情况)。

效果bless是它允许您将特殊语法应用于祝福引用。

例如,如果一个受祝福的引用存储在$objbless与包“Class”相关联)中,那么$obj->foo(@args)将调用一个子例程foo并将引用作为第一个参数传递,$obj然后是其余参数(@args)。子程序应该在包“Class”中定义。如果包“Class”中没有子例程foo,则将搜索其他包的列表(取自包“Class”中的数组)并调用找到@ISA的第一个子例程。foo

于 2008-12-24T20:39:53.623 回答
10

短版本:它将哈希标记为附加到当前包命名空间(以便该包提供其类实现)。

于 2008-12-24T19:54:06.447 回答
9

这个函数告诉 REF 引用的实体它现在是 CLASSNAME 包中的一个对象,或者如果 CLASSNAME 被省略,则为当前包。建议使用 bless 的两个参数形式。

示例

bless REF, CLASSNAME
bless REF

返回值

此函数返回对 CLASSNAME 中祝福的对象的引用。

示例

以下是显示其基本用法的示例代码,对象引用是通过祝福对包类的引用来创建的 -</p>

#!/usr/bin/perl

package Person;
sub new
{
    my $class = shift;
    my $self = {
        _firstName => shift,
        _lastName  => shift,
        _ssn       => shift,
    };
    # Print all the values just for clarification.
    print "First Name is $self->{_firstName}\n";
    print "Last Name is $self->{_lastName}\n";
    print "SSN is $self->{_ssn}\n";
    bless $self, $class;
    return $self;
}
于 2016-06-03T12:01:43.027 回答
4

我将在这里提供一个答案,因为这里的那些并没有完全点击我。

Perl 的 bless 函数将任何对包内所有函数的引用关联起来。

为什么我们需要这个?

让我们从用 JavaScript 表达一个例子开始:

(() => {
    'use strict';

    class Animal {
        constructor(args) {
            this.name = args.name;
            this.sound = args.sound;
        }
    }

    /* [WRONG] (global scope corruption)
     * var animal = Animal({
     *     'name': 'Jeff',
     *     'sound': 'bark'
     * }); 
     * console.log(animal.name + ', ' + animal.sound); // seems good
     * console.log(window.name); // my window's name is Jeff?
     */

    // new is important!
    var animal = new Animal(
        'name': 'Jeff',   
        'sound': 'bark'
    );

    console.log(animal.name + ', ' + animal.sound); // still fine.
    console.log(window.name); // undefined
})();

现在让我们剥离类结构并没有它:

(() => {
    'use strict';

    var Animal = function(args) {
        this.name = args.name;
        this.sound = args.sound;
        return this; // implicit context hashmap
    };

    // the "new" causes the Animal to be unbound from global context, and 
    // rebinds it to an empty hash map before being constructed. The state is
    // now bound to animal, not the global scope.
    var animal = new Animal({
        'name': 'Jeff',
        'sound': 'bark'
    });
    console.log(animal.sound);    
})();

该函数采用无序属性的哈希表(因为在 2016 年必须以特定顺序在动态语言中编写属性是没有意义的)并返回具有这些属性的哈希表,或者如果您忘记输入新关键字,它将返回整个全局上下文(例如浏览器中的窗口或 nodejs 中的全局)。

Perl 没有“this”、“new”或“class”,但它仍然可以有一个行为相似的函数。我们不会有构造函数或原型,但我们将能够随意创建新动物并修改它们的个别属性。

# self contained scope 
(sub {
    my $Animal = (sub {
        return {
            'name' => $_[0]{'name'},
            'sound' => $_[0]{'sound'}
        };
    });

    my $animal = $Animal->({
        'name' => 'Jeff',
        'sound' => 'bark'
    });

    print $animal->{sound};
})->();

现在,我们有一个问题:如果我们希望动物自己发出声音,而不是我们打印它们的声音,该怎么办。也就是说,我们需要一个函数 performSound 来打印动物自己的声音。

做到这一点的一种方法是教每个动物如何发出声音。这意味着每只 Cat 都有自己的重复函数来执行声音。

# self contained scope 
(sub {
    my $Animal = (sub {
        $name = $_[0]{'name'};
        $sound = $_[0]{'sound'};

        return {
            'name' => $name,
            'sound' => $sound,
            'performSound' => sub {
                print $sound . "\n";
            }
        };
    });

    my $animal = $Animal->({
        'name' => 'Jeff',
        'sound' => 'bark'
    });

    $animal->{'performSound'}();
})->();

这很糟糕,因为每次构造动物时,performSound 都会作为一个全新的函数对象放入。10000 只动物意味着 10000 种表演声音。我们希望有一个函数 performSound,所有动物都可以使用它来查找自己的声音并打印出来。

(() => {
    'use strict';

    /* a function that creates an Animal constructor which can be used to create animals */
    var Animal = (() => {
        /* function is important, as fat arrow does not have "this" and will not be bound to Animal. */
        var InnerAnimal = function(args) {
            this.name = args.name;
            this.sound = args.sound;
        };
        /* defined once and all animals use the same single function call */
        InnerAnimal.prototype.performSound = function() {
            console.log(this.name);
        };

        return InnerAnimal;
    })();

    /* we're gonna create an animal with arguments in different order
       because we want to be edgy. */
    var animal = new Animal({
        'sound': 'bark',
        'name': 'Jeff'
    });
    animal.performSound(); // Jeff
})();

这就是与 Perl 的相似之处。

JavaScript 的 new 运算符不是可选的,没有它,对象方法中的“this”会破坏全局范围:

(() => {
    // 'use strict'; // uncommenting this prevents corruption and raises an error instead.

    var Person = function() {
        this.name = "Sam";
    };
//    var wrong = Person(); // oops! we have overwritten window.name or global.main.
//    console.log(window.name); // my window's name is Sam?
    var correct = new Person; // person's name is actually stored in the person now.

})();

我们希望每个 Animal 都有一个函数来查找该动物自己的声音,而不是在构造时对其进行硬编码。

Blessing 让我们可以使用一个包作为对象的原型。这样,对象就知道它被“引用”的“包”,进而可以让包中的函数“进入”从该“包对象”的构造函数创建的特定实例:

package Animal;
sub new {
    my $packageRef = $_[0];
    my $name = $_[1]->{'name'};
    my $sound = $_[1]->{'sound'};

    my $this = {
        'name' => $name,
        'sound' => $sound
    };   

    bless($this, $packageRef);
    return $this;
}

# all animals use the same performSound to look up their sound.
sub performSound {
    my $this = shift;
    my $sound = $this->{'sound'};
    print $sound . "\n";
}

package main;
my $animal = Animal->new({
    'name' => 'Cat',
    'sound' => 'meow'
});
$animal->performSound();

摘要/ TL;博士

Perl 没有“this”、“class”和“new”。将一个对象祝福到一个包给该对象一个对该包的引用,并且当它调用包中的函数时,它们的参数将偏移1个槽,第一个参数($_ [0]或shift)将等效于javascript的“这个”。反过来,您可以在某种程度上模拟 JavaScript 的原型模型。

不幸的是,(据我所知)在运行时创建“新类”是不可能的,因为您需要每个“类”都有自己的包,而在 javascript 中,您根本不需要包,因为“new”关键字组成一个匿名哈希图,供您在运行时用作一个包,您可以在其中添加新函数和动态删除函数。

有一些 Perl 库创建了自己的方法来弥补这种表达能力的限制,例如 Moose。

为什么会出现混乱?

因为包裹。我们的直觉告诉我们将对象绑定到包含其原型的 hashmap。这让我们可以像 JavaScript 一样在运行时创建“包”。Perl 没有这样的灵活性(至少不是内置的,你必须发明它或从其他模块中获取它),进而阻碍你的运行时表现力。称它为“祝福”也没有多大好处。

我们想做的

像这样的东西,但是绑定到原型映射递归,并且被隐式绑定到原型而不是必须显式地这样做。

这是一个天真的尝试:问题是“调用”不知道“调用它的东西”,所以它也可能是一个通用的 perl 函数“objectInvokeMethod(object, method)”,它检查对象是否具有方法,或者它的原型有它,或者它的原型有它,直到它到达最后并找到它与否(原型继承)。Perl 有很好的 eval 魔法来做这件事,但我会把它留给我以后可以尝试做的事情。

无论如何,这是一个想法:

(sub {

    my $Animal = (sub {
        my $AnimalPrototype = {
            'performSound' => sub {
                return $_[0]->{'sound'};
            }
        };

        my $call = sub {
            my $this = $_[0];
            my $proc = $_[1];

            if (exists $this->{$proc}) {
                return $this->{$proc}->();
            } else {
                return $this->{prototype}->{$proc}->($this, $proc);
            }
        };

        return sub {
            my $name = $_[0]->{name};
            my $sound = $_[0]->{sound};

            my $this = { 
                'this' => $this,
                'name' => $name,
                'sound' => $sound,
                'prototype' => $AnimalPrototype,
                'call' => $call                
            };
        };
    })->();

    my $animal = $Animal->({
        'name' => 'Jeff',
        'sound'=> 'bark'
    });
    print($animal->{call}($animal, 'performSound'));
})->();

无论如何,希望有人会发现这篇文章很有用。

于 2016-08-26T22:24:42.173 回答
3

内部特别区分bless-ed 引用的是SV for 引用(存储在标量中)获取一个FLAGS附加值(OBJECT),以及STASH带有包名称的 a (有一些其他区别)

perl -MDevel::Peek -wE'
    package Pack  { sub func { return { a=>1 } } }; 
    package Class { sub new  { return bless { A=>10 } } }; 
    $vp  = Pack::func(); print Dump $vp;   say"---"; 
    $obj = Class->new;   print Dump $obj'

印刷品,相同的(与此无关的)部分被抑制

SV = IV(0x12d5530) at 0x12d5540
  REFCNT = 1
  FLAGS = (ROK)
  RV = 0x12a5a68
  SV = PVHV(0x12ab980) at 0x12a5a68
    REFCNT = 1
    FLAGS = (SHAREKEYS)
    ...
      SV = IV(0x12a5ce0) at 0x12a5cf0
      REFCNT = 1
      FLAGS = (IOK,pIOK)
      IV = 1
---
SV = IV(0x12cb8b8) at 0x12cb8c8
  REFCNT = 1
  FLAGS = (PADMY,ROK)
  RV = 0x12c26b0
  SV = PVHV(0x12aba00) at 0x12c26b0
    REFCNT = 1
    FLAGS = (OBJECT,SHAREKEYS)
    STASH = 0x12d5300   "Class"
    ...
      SV = IV(0x12c26b8) at 0x12c26c8
      REFCNT = 1
      FLAGS = (IOK,pIOK)
      IV = 10

这样,口译员就知道

  • 这是一个对象

  • 它属于什么包

这通知了它的使用。

例如,当遇到对该变量的取消引用时($obj->name),在包(或层次结构)中寻找具有该名称的子,将对象作为第一个参数传递等。

于 2019-11-21T08:51:55.567 回答
0

我按照这个思路来指导开发面向对象的Perl。

Bless 将任何数据结构引用与类相关联。鉴于 Perl 如何创建继承结构(在一种树中),很容易利用对象模型来创建用于组合的对象。

对于我们称为对象的这种关联,开发时始终牢记对象的内部状态和类行为是分开的。您可以祝福/允许任何数据引用使用任何包/类行为。由于包可以理解对象的“情绪”状态。

于 2015-05-05T10:59:32.133 回答
-9

例如,如果您确信任何 Bug 对象都将是一个祝福哈希,那么您可以(终于!)在 Bug::print_me 方法中填写缺失的代码:

 package Bug;
 sub print_me
 {
     my ($self) = @_;
     print "ID: $self->{id}\n";
     print "$self->{descr}\n";
     print "(Note: problem is fatal)\n" if $self->{type} eq "fatal";
 }

现在,每当 print_me 方法通过对已被祝福到 Bug 类中的任何哈希的引用来调用时,$self 变量都会提取作为第一个参数传递的引用,然后打印语句访问祝福哈希的各个条目。

于 2011-06-16T05:39:25.810 回答