On 8/12/07, Chris Pax <[EMAIL PROTECTED]> wrote:
snip
> so I tried require and do.
> I guess its best to explain my goal here. I am using perl for gtk
> programming. The main code will use the glade bindings and use a
> separate file for call backs. I want to make it so that if a new
> callback/function is defined in the glade file, that function will be
> appended to the callbacks file, with the 1; at the end. this way, you
> don't half to keep on doing it your self or running some script.
snip

I do not suggest doing this.  It is better to wrap glade-2 (or
whatever you use to edit your glade files) with a script that runs an
xml parser to pull out the names of the defined signals and edits the
proper module.  If you do it the way you describe, then you have to
try to exercise every signal that can possibly be called and,
undoubtedly, you will eventually miss one.

But, if you have your heart set on doing it, then the best way to
handle this is with Perl's AUTOLOAD* functionality.  Basically it
allows you to create one catchall function or method (per package)
that will be called if perl can't find an appropriate function or
method to call.  Inside of this function you can see the fully
qualified name of the function or method that should have been called.
 It is easy enough to write to the modules file within the AUTOLOAD
function, so you just write to it then.  There is no need to reload
the module because  the AUTOLOAD function will just keep catching the
non-existent function or method; however, you will need to put some
logic in place to keep it from constantly adding the function.  I have
implemented an example of this with three files: Funcs.pm, p.pl, and
project1.glade.  Note, you could even go so far as to make the
AUTOLOAD function popup another top-level window, let you type in the
code that should go with that widget, and then save the full function
to the module instead of using a constant string like I did, but I
leave that as an exercise for the reader.

* http://perldoc.perl.org/perlsub.html#Autoloading-autoloading-AUTOLOAD

**** Funcs.pm ****
package Funcs;

use strict;
use warnings;

use Gtk2;

BEGIN {
        our $AUTOLOAD;
        our $module;
        our %found;

        #find first version of this module
        for my $dir (@INC) {
                if (-f "$dir/Funcs.pm") {
                        $module = "$dir/Funcs.pm";
                        last;
                }
        }
}


sub on_window_destroy {
        Gtk2->main_quit;
}

sub on_button1_clicked {
        print "this func is implemented\n";
        return 1;
}

sub AUTOLOAD {
        our $AUTOLOAD;
        our $module;
        our %found;
        print "glade tried to call $AUTOLOAD, but it doesn't exist\n";
        unless ($found{$AUTOLOAD}) {
                print "writing $AUTOLOAD to $module\n";
                open my $mod, '>>', $module
                        or die "could not open $module: $!";
                my ($func) = $AUTOLOAD =~ /.*::([a-zA-Z_]\w*)/;
                print $mod "\nsub $func { return 1; }\n";
                $found{$AUTOLOAD} = 1;
        }

        return 1;
}

1;
**** p.pl ****
#!/usr/bin/perl

use strict;
use warnings;

use Gtk2;
use Gtk2::GladeXML;
use Funcs;

Gtk2->init;

my $glade = Gtk2::GladeXML->new("./project1.glade");
$glade->signal_autoconnect_from_package('Funcs');

Gtk2->main;
**** project1.glade ****
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd";>

<glade-interface>

<widget class="GtkWindow" id="window">
  <property name="visible">True</property>
  <property name="title" translatable="yes">window</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="focus_on_map">True</property>
  <property name="urgency_hint">False</property>
  <signal name="destroy" handler="on_window_destroy"
last_modification_time="Mon, 13 Aug 2007 20:46:26 GMT"/>

  <child>
    <widget class="GtkHBox" id="hbox1">
      <property name="visible">True</property>
      <property name="homogeneous">True</property>
      <property name="spacing">0</property>

      <child>
        <widget class="GtkButton" id="button1">
          <property name="visible">True</property>
          <property name="can_focus">True</property>
          <property name="label" translatable="yes">button1</property>
          <property name="use_underline">True</property>
          <property name="relief">GTK_RELIEF_NORMAL</property>
          <property name="focus_on_click">True</property>
          <signal name="clicked" handler="on_button1_clicked"
last_modification_time="Mon, 13 Aug 2007 20:32:02 GMT"/>
        </widget>
        <packing>
          <property name="padding">0</property>
          <property name="expand">True</property>
          <property name="fill">True</property>
        </packing>
      </child>

      <child>
        <widget class="GtkButton" id="button2">
          <property name="visible">True</property>
          <property name="can_focus">True</property>
          <property name="label" translatable="yes">button2</property>
          <property name="use_underline">True</property>
          <property name="relief">GTK_RELIEF_NORMAL</property>
          <property name="focus_on_click">True</property>
          <signal name="clicked" handler="on_button2_clicked"
last_modification_time="Mon, 13 Aug 2007 20:32:08 GMT"/>
        </widget>
        <packing>
          <property name="padding">0</property>
          <property name="expand">True</property>
          <property name="fill">True</property>
        </packing>
      </child>

      <child>
        <widget class="GtkButton" id="button3">
          <property name="visible">True</property>
          <property name="can_focus">True</property>
          <property name="label" translatable="yes">button3</property>
          <property name="use_underline">True</property>
          <property name="relief">GTK_RELIEF_NORMAL</property>
          <property name="focus_on_click">True</property>
          <signal name="clicked" handler="on_button3_clicked"
last_modification_time="Mon, 13 Aug 2007 20:32:14 GMT"/>
        </widget>
        <packing>
          <property name="padding">0</property>
          <property name="expand">True</property>
          <property name="fill">True</property>
        </packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to