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
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/