Forum Moderators: coopster & phranque

Message Too Old, No Replies

"requiring" in perl. Any way to get a reinterp?

         

Brett_Tabke

7:56 am on May 3, 2001 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



If you use "require script.pl", perl will only include the file one time.
Is there anyway to force it to "rerequire" the file?

I can think of all kinds of alternatives such as loading it in an eval, or making the external wrapped in a subroutine. I'd like to be able to just merely get it to reinterp the file if possible. Is there a way I don't know about?

sugarkane

10:30 pm on May 3, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Brett, I'm not sure I'm understanding you here, but:

Do you want to, for example, run an external piece of code, change a few variables and then run it again?

Brett_Tabke

9:14 am on May 4, 2001 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



require "foo.cgi"
Then later I want to do the same again.

There are global variables in the foo.cgi, that will get walked on by other portions of the code. I want to be able to bring those variables back in and reset them.

The actual code I'm working on is the code that generates threads right here. I want to keep the user files in stock perl code:

#z WebmasterWorld User Access file version 2
$username="sugarkane";
$password="zzzzzz";
$membertype="member";
$access_level="9";
$moderator="13";
$emailaddress="***@***";
$showaddress="yes";
$joined="962923553";
$memberlastpost="987806690";
$memberposts="207";
$homepage="";
$interests="Cookery, wine, travel, music, BWFC";
$occupation="Web development and promotion";
$location="Manchester, UK";
$membericqnumber="";
$picture="http://www.cobweb.f9.co.uk/designs/me.jpg";
$form_input_width="70";
$gmtoffset="25";
$time_zone_id="bst";
$pvtnote="";
$sticky_active_folder="inbox";
$sticky_inbox="";
$sticky_unread="";
$sticky_todo_check="";
$sticky_last_check="";
$autonotify="";
$allowedforums="**¦**¦**¦**";
$realname="";
$lastread="";
$res11="";
$res12="";
$res13="";
$res14="";
$res15="";
$res16="";
$res17="";
1;
----------------

Why would I want it that way? Speed, ease of maint, ease of portability, ease of updating. Why do by hand, what the system can do 4 times as fast?

Now, the only problem in the whole setup? Trying to get it to reinterp those values later when I need them in other sections of code. The current stumbling block is the message displays here where, each users account is read in on post display (joined, posts, member type, etc).

I think the only answer is to wrap the files in subroutines:

# WebmasterWorld User Access file version 2
sub sugarkane {
$username='sugarkane';
$password='i don't think so;
.....
}

Then later, I could just &$username to bring it back to life.

Air

1:14 pm on May 4, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I understand what you are doing correctly, the following would set the variables to their then current value and you could just reference them in your main script without a subroutine call.

eval {
require "some_code.pl";
};

Brett_Tabke

4:30 pm on May 4, 2001 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Speed. I'm sure you know eval is really slow. It would be quicker to throw it into a flat file than use an eval. I'm trying to do this for speed and let the system load routines take over.

Now that I've spent a day working on it and seeing the problems, I think I'll scrap it and go back to the flat file system.

The big benifit of the "data as a program" route, was the ease of adding future variables and the speed.

littleman

4:49 pm on May 4, 2001 (gmt 0)



How about throwing the variables into a hash?
You could put it into a hash and then do something like
my %hash2 = %hash1;
You could do what you will with hash2, and then still be able to call up hash1 as needed.

sugarkane

6:36 pm on May 4, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know how this is for speed, but it was fun coming up with it... I think it does what you want.

First, change the data file to:

package sugarkane;
sub reset {
$main::username="sugarkane";
$main::password="foobarbaz";
# etc
}
1;

Then, you can reset the global variables to the values for each user at any time with something like this:

#!/usr/bin/perl
$username="foo";
print "$username\n";

$user->reset();
print "$username\n";
exit;