On Aug 12, 12:17 pm, [EMAIL PROTECTED] (Mr. Shawn H. Corey) wrote:
> Paul Lalli wrote:
> > On Aug 11, 7:18 pm, [EMAIL PROTECTED] (Chris Pax) wrote:
> >> i have two files
>
> >> ###########callbacks.pm###############
> >> sub foo{
> >>      return "foo";
> >> }
>
> >> sub bar{
> >>      return "foo";
> >> }
>
> >> 1;
> >> #################
>
> >> ############main.pl##############
> >> #!/usr/bin/perl
>
> >> #PSUDO CODE#
> >> #open callbacks.pm
> >> #remove 1;
> >> #write "sub moo{\n\treturn \"moo\";\n}\n"
> >> #write "1;"
>
> >> package mainprog;
> >> use callbacks;
>
> >> print foo();
> >> print bar();
> >> print moo();
> >> 1;
> >> ###################3
>
> >> you see here what i want to happen. what I am doing is writing a new
> >> function to the callbacks file, then I want to import the file.
>
> > You have a problem here unrelated to your desire to re-write the
> > module.  You are declaring subroutines in the package main, but trying
> > to call them in package mainprog.  Perl won't be able to find those
> > subroutines regardless.
>
> Since the use statement comes after the package statement, the subroutines 
> are add to that package, not the main.  The program will work up to the 
> subroutine moo, which is not defined.
>
>
>
> > Your problem that actually has to do with the question is that you're
> > using 'use'.   'use' happens at compile time.  That means it happens
> > long before the code that re-writes callbacks.pm is executed.  You
> > need 'require' instead.
>
> > perldoc -f use
> > perldoc -f require
>
> Actually, 'require' only loads the file once.  Since the OP mentioned the 
> python function reload, I thought 'do' would be more appropriate.
>
> $ cat script.pl
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> our $count = 0;
> print "via require...\n";
> for ( 1 .. 10 ){
>   require 'perllib.pl';
>
> }
>
> print "via do...\n";
> $count = 0;
> for ( 1 .. 10 ){
>   do 'perllib.pl';}
>
> __END__
> $ cat perllib.pl
> #!/
>
> use strict;
> use warnings;
>
> our $count;
> print "\tinc count: ", $count ++, "\n";
>
> 1;
> __END__
>
> ###
>
> Writing a subroutine to the end of a file would mean that each time the 
> program is run, a new subroutine is added.  The OP probably wants something 
> like eval (See `perldoc -f eval`) but even that is a last resort.  Perl is 
> powerful enough to do almost everything you want without dynamic programming. 
>  It's only on rare occasions that you have to resort to it.
>
> --
> Just my 0.00000002 million dollars worth,
>   Shawn
>
> "For the things we have to learn before we can do them, we learn by doing 
> them."
>   Aristotle
thanks you all.

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.

####################### calculator.pl###################

#!/usr/bin/perl
####################################
### Main package ##
####################################

my $path =  $0 ;
sub get_old_callbacks{
        open CALLS, "calls.pm";
        my @calls;
        while(<CALLS>){
                chomp;
                if(/sub/){
                        s/sub //;
                        s/{//;
                        #print $_."\n";
                        push @calls, $_;
                }
        }
        close CALLS;
        return @calls;
}
sub in{
        #returns true or false
        $pin = shift @_;
        @heystack = @_;
        foreach $item (@heystack){
                if ($pin eq $item){
                        return true;
                }
        }
        return false;
}

sub get_new_callbacks{
        open SIGNAL, "calculator.glade";
        my %signals;
        @oldsignals = get_old_callbacks();
        #foreach $osig(@oldsignals){
        #       $signals{$osig}="";
        #}
        while (<SIGNAL>){
                chomp;
                if(/<signal.*handler="/){
                        #print "Matched: |$'|\n";
                        $_=$';
                        if(/\"\/\>/){
                                #print $`."\n";
                                if(not in("$`",@oldsignals)){
                                        $signals{$`}="";
                                }
                        }
                }
        }
        close SIGNAL;
        return keys %signals;
}

sub write_new_callbacks{
        open CALLBACKS, "calculatorCallbacks.pm";
        @newcalls=get_new_callbacks();
        my $data="";
        select POOP;
        while (<CALLBACKS>){
                if (/1;/){
                        s/1;//;
                }
                #print "$_\n";
                print $_;
                $data.=$_;
        }
        foreach $call(@newcalls){
                $data .= "\nsub $call\{\nprint \"this is function 
'$call\'\";\n}\n";
        }
        $data .= "\n1;\n";
        close CALLBACKS;
        open CALLBACKS, ">calculatorCallbacks.pm";
        print CALLBACKS $data;
        close CALLBACKS;

}

write_new_callbacks();

package mainprog;
use File::Basename;
use lib dirname( $0 );
use calculatorCallbacks;
use strict ;
require Exporter;
use vars qw(@EXPORT_OK);
@EXPORT_OK = qw ($gladexml);
use vars qw($gladexml);
use Gtk2 "-init";
use Gtk2::GladeXML;


$gladexml = Gtk2::GladeXML->new(dirname( $path )."/calculator.glade");
calculatorCallbacks::init ($gladexml);
$gladexml->signal_autoconnect_from_package("calculatorCallbacks");
1 ;
Gtk2->main;
########################################################################

#########################calculatorCallbacks.pm#############################


################################
### Callback functions            ##
################################
use strict;

package calculatorCallbacks;

use vars qw($gladexml);

sub init{
        print "initlize\n";
}

sub on_quit_activate{
        print "function 'on_quit_activate' not implmented\n";
}
sub on_fowardinhistory_activate{
        print "function 'on_fowardinhistory_activate' not implmented\n";
}
sub on_about_dialog_activate{
        print "function 'on_about_dialog_activate' not implmented\n";
}
sub on_button_press{
        print "function 'on_button_press' not implmented\n";
}
sub on_backinhistory_activate{
        print "function 'on_backinhistory_activate' not implmented\n";
}

1;
######################################

if you can solve the riddle, that would be great.
as for now I will read up on all the suggestions made.

thanks again


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


Reply via email to