perl - Why is my Moo object that inherits from a non-Moo class blessed into the parent's package for some modules? -
i'm trying create gtk3 application in perl using gobject introspection , moo. there's non-moo class gtk, gtk::applicationwindow
, subclass through moo using extends 'gtk::applicationwindow'
. issue when object of subclass created, remains of type of parent class - i.e. gtk::applicationwindow
.
i tried same thing subclassing own non-moo based class instead, , object created subclass of correct type. reason difference?
use v5.10; use strict; use warnings; # import gtk classes (non-moo) use glib::object::introspection; glib::object::introspection->setup(basename => 'gtk', version => '3.0', package => 'gtk'); glib::object::introspection->setup(basename => 'gio', version => '2.0', package => 'gio'); ################################################# { # dummy non-moo class package classnonmoo; sub new { bless {}, shift; } } { # moo class extending dummy class package classmoo; use moo; extends 'classnonmoo'; sub foreignbuildargs { ($class, %args) = @_; return ($args{app}); } } ################################################# { # moo class extending gtk::applicationwindow package classmoogtkappwin; use moo; extends 'gtk::applicationwindow'; sub foreignbuildargs { ($class, %args) = @_; return ($args{app}); } } ################################################# # create objects of classmoo , classmoogtkappwin sub create_objects { ($app) = @_; $o1 = classmoo->new( app => $app ); $o2 = classmoogtkappwin->new( app => $app ); "o1 = $o1\no2 = $o2"; # output: # o1 = classmoo=hash(0x2f7bc50) # o2 = gtk::applicationwindow=hash(0x2f7bd40) # # shouldn't o2 of type classmoogtkappwin ? exit(0); } # can create gtkapplicationwindow after creating gtkapplication , # running it. code ensures create_object() called once # application 'active'. $app = gtk::application->new('org.test', 'flags-none'); $app->signal_connect(activate => sub { create_objects($app) }); $app->run();
"new" constructors need written in way observe caller's actual class (which works subclassing), can hard-code class create objects with.
compare:
package myclass; # considerate of subclassing sub new { $class = shift; return bless {}, $class; } # doesn't give shit sub new { $class = shift; return bless {}; }
glib looks it's xs module wrapper around c library, , might hard code class.
you try re-bless (call bless again on object constructed) object actual subclass. not sure how work moo, in build method.
you skip inheritance , use delegation instead. create attribute hold original window object, , delegate all methods it, except own in subclass.
in moose (not moo), can regex: https://metacpan.org/pod/moose#handles-array-hash-regexp-role-roletype-ducktype-code
not sure how nicely moo.
Comments
Post a Comment