Glib::Object::Subclass and Exporter
- From: Domovoy <domovoy errlock org>
- To: gtk-perl-list gnome org
- Subject: Glib::Object::Subclass and Exporter
- Date: Sat, 08 Sep 2012 01:31:46 +0200
Hi, i'm not very used to mailing-lists, so if i do anything wrong,
please correct me.
So, i've been struggling a long time before i could get
Glib::Object::Subclass and Exporter to work together.
If there is a known way to do this, please share your knowledge.
perl version: 5.14.2-12
gtk version: 2.24.10-2
gtk-perl version: 2:1.244-1
The problem:
Either i setup Exporter before Glib::Object::Subclass, and then nothing
is exported from my module when i use it (just as if i didn't use
Exporter).
package Foo::Bar;
use strict;
use warnings;
BEGIN {
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(SOME_CONST);
}
use Gtk2;
use Glib::Object::Subclass
Gtk2::ScrolledWindow::;
Or i setup Glib::Object::Subclass before Exporter, and then i get my
exports, but also errors from Gtk like 'Can't locate object method "new"
via package "Foo::Bar::SUPER"':
package Foo::Bar;
use strict;
use warnings;
use Gtk2;
use Glib::Object::Subclass
Gtk2::ScrolledWindow::;
BEGIN {
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(SOME_CONST);
}
sub new {
my $class = shift;
my $self = $class->SUPER::new();
bless($self, $class);
#
# Do some stuff that can't be done in INIT_INSTANCE
#
return $self;
}
So, i finally took a look at Glib::Object::Subclass.pm, and saw that it
was doing its job by defining an import sub.
By adding some print statements there, i could point that it was called
when i use Glib::Object::Subclass (no problem there), but also when i
use my package (and then, my package Exporter sub is not called).
Why is this one called instead of Exporter one is beyond my
understanding of perl, but anyway Glib::Object::Subclass just discards
calls to it's import that are not directed to it, ignoring any stuff the
user might need from an import sub.
I then changed it like this:
sub import {
my $package = $_[0];
if($package ne __PACKAGE__) {
# This is not our import
if($package->can('export_to_level')) {
$package->export_to_level(1, @_);
}
return;
}
my ($self, $superclass, %arg) = @_;
my $class = caller;
Glib::Type->register_object(
$superclass, $class,
%arg,
);
# ensure that we have a perlish new(). the old version of this
# code used a CHECK block to put a new() in if we didn't already
# have one in the package, but it may be too late to run a CHECK
# block when we get here. so, we use the old-fashioned way...
unshift @{ $class."::ISA" }, __PACKAGE__;
}
So now, if it detects that import as been called for another package, it
finds out if Exporter is used in that package
( $package->can('export_to_level') ), and then relay the request to it
( $package->export_to_level(1, @_); ).
By doing this, my exports works well, and so are my Gtk objects.
So, i'm not a perl guru, and as i said in the beginning of this mail
there may be a know way to deal with it and i'm just messing with stuff
i shouldn't here.
But anyway, either i need to be pointed to the right way to this, or
this may be a matter of improvement of gtk-perl code.
Thanks for reading, this is a long mail, but i wanted to be as clear as
possible.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]