Skip to content

Instantly share code, notes, and snippets.

@prakashk
Created October 1, 2013 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prakashk/6783710 to your computer and use it in GitHub Desktop.
Save prakashk/6783710 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use v5.14;
use mop;
use Data::Printer;
class Foo {
has $!foo is ro;
method new($class: $foo) {
say "# Foo::new($foo)";
$class->next::method(foo => $foo);
}
submethod BUILD($args) {
say "# Foo::BUILD(", p($args), ")";
$!foo = $args->{foo} if exists $args->{foo};
}
}
class Bar extends Foo {
has $!bar is ro;
method new($class: $foo, $bar) {
say "# Bar::new($foo, $bar)";
$class->next::method($foo);
}
submethod BUILD($args) {
say "# Bar::BUILD(", p($args), ")";
$!bar = $args->{bar} if exists $args->{bar};
}
}
my $bar = Bar->new(10, 42);
p mop::dump_object($bar);
@prakashk
Copy link
Author

prakashk commented Oct 1, 2013

Output:

# Bar::new(10, 42)
# Foo::new(10)
# Foo::BUILD(\ {
    foo   10
})
# Bar::BUILD(\ {
    foo   10
})
\ {
    $!bar       undef,
    __CLASS__   "Bar",
    $!foo       10,
    __ID__      24118488,
    __SELF__    Bar  {
        public methods (0)
        private methods (0)
        internals: undef
    }
}

What should change in order to intialize Bar object properly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment