I Have a Secret Too

chromatic on 2008-12-12T02:55:16

What do you think this code should produce?

use Test::More tests => 6;

{
    class Foo;

    sub new { bless {}, shift }
    sub WHO { 'foo' }
}

class Bar {
    sub new { bless {}, shift }
    sub WHO { 'bar' }

    ::is( __PACKAGE__, 'Bar',
        'class name block should set package name in block'  );
}

is( __PACKAGE__, 'main', 'class name should not leak out of block' );

my $foo = Foo->new();
my $bar = Bar->new();

is( $foo->isa( 'Foo' ), 1, 'Foo constructor should work' );
is( $foo->WHO(), 'foo', '... in the right package' );
is( $bar->isa( 'Bar' ), 1, 'Bar constructor should work' );
is( $bar->WHO(), 'bar', '... in the right package' );

Oh, yes. This is Perl 5 code.


sign me up

Eric Wilhelm on 2008-12-12T07:53:37

I find this Perl intriguing and I would like to have it in my interpreter.

Yak.

bacek on 2008-12-12T09:38:52

Do we really need perl6? :)

MooseX::Declare

rafl on 2008-12-12T10:58:39

MooseX::Declare provides similar features. The class keyword only accepts a block with the class definition right now, but i'm also working on implementing "class Foo;" using B::Hooks::OP::Check::StashChange.

I'd be glad if we could have some basic syntax compatibility between MooseX::Declare and your class keyword patch, in case you want to add some kind of options to it so things like "class Foo extends Bar { .. }" and similar would be possible.