[% setvar title PRAYER - what gets said when you C something %]

This file is part of the Perl 6 Archive

Note: these documents may be out of date. Do not use as reference!

To see what is currently happening visit http://www.perl6.org/

TITLE

PRAYER - what gets said when you bless something

VERSION

  Maintainer: Simon Cozens <simon@brecon.co.uk>
  Date: 25 Sep 2000
  Last Modified: 25 Sep 2000
  Mailing List: perl6-language-objects@perl.org
  Number: 307
  Version: 2
  Status: Withdrawn

NOTES ON WITHDRAWL

Damian Conway noted:

	RFC 189 covers this.

ABSTRACT

This RFC proposes a special sub, PRAYER, which is automatically called on blessing.

DESCRIPTION

The abstract more or less says it all, but this is more than just a joke. Suppose, for instance, your class needs to do some initialisation whenever a new object is created; PRAYER would be where the class initialisation takes place.

There's also the more interesting and (IMHO) important issue of reblessing; that is, changing the class of an object. There are times when you want to do this, but it's hairy because you have to know the workings of both classes. However, if you have PRAYER called automatically on blessing, you can automagically "cast" objects between classes. For instance, an object in class X would get changed to an object in class Y with

    bless $obj, "Y";

Perl would then perform the reblessing and call:

    package Y;
    sub PRAYER ($object, $oldclass) {
        if ($oldclass eq "X") {
            ...
        } else {
            die "Can't cast an $oldclass to a Y";
        }
    }

You may also use this method as a form of "pre-constructor", or even, on simple enough classes, a constructor proper.

Maybe subs to cast between classes should somehow be shared between the two classes; the special sub INTERCESSION would be the obvious place for that.

IMPLEMENTATION

Adding a method call to the end of bless should not be tricky.

REFERENCES

None.