Wiggins d Anconia wrote:
>> Folks,
>> 
>> I've run into a couple of issues with use and was hoping someone
>> could help me come up with a solution.
>> 
>> First, I use a specific module in a work environment to set some
>> global variables. I want to re-use my code in another environment
>> which doesn't have the specific configuration module. I don't want to
>> have to keep two separate copies of the code. What I currently use in
>> the work environment is of the format:
>> 
>> use lib '/path/to/work/modules';
>> use Prefs;
>> 
>> my $config = Prefs->new();
>> my $param1 = $config->get("PARAM1");
>> 
>> I'd like to something of the format:
>> 
>> my $param1;
>> if (-f '/path/to/work/modules/Prefs.pm') {
>>      use lib '/path/to/work/modules';
>>      use Prefs;
>> 
>>      my $config = Prefs->new();
>>      $param1 = $config->get("PARAM1");
>> }
>> else {
>>      $param1 = "default value here";
>> }
>> 
>> Is something like this possible?
>> 
> 
> perldoc -f eval  (2nd form)
> 
> Generally you use 'eval' to wrap a segment of code that can die, then
> just catch the error and set your default when an error occurs. So in
> this case if the module isn't available, can't be loaded (think
> syntax error in it), or causes some other error, then the failure
> will be caught and you can move on gracefully.    
> 
> If the examples in the doc aren't sufficient, come back and someone
> will provide a good example. 
> 
>> Also, some of the config-style modules I use store their
>> configuration values with the module itself. Using the above example
>> Prefs.pm, the function $config->get("PARAM1") might look like:
>> 
>> my $self = shift;
>> my $val = shift;
>> return $self->{DATA}{$val};
>> 
>> I have code which runs daemonized (using Proc::Daemon) that begins by
>> using the Prefs config module. It's quite possible that the
>> parameters in the Prefs config module get modified. I may need my
>> daemonized code to "refresh" its copy of the Prefs config module to
>> pull in the new parameters. Can this be done, or do I have to stop
>> the daemon and restart it? 
>> 
>> Thanks all!
>> - Ed
>> 
> 
> perldoc -f do
> 
> There is an example of this very thing in that doc. In the case of
> your daemon, usually you would set up a signal handler, catch the
> signal (usually HUP) and re-init the config information when that
> signal is received.   
> 
> perldoc perlipc (for more about signal handling)
> 
> There is excellent information about daemons in the Network
> Programming with Perl book, I highly recommend it if you have the
> resources and will be writing code of this nature.  
> 
> http://danconia.org

Wiggins,

Thanks for the help! I was hung-up on the notion that every module I
loaded had to be loaded at compile-time. By using the "do" function, I
can optionally load and/or reload my modules at run-time. My code now
looks like:

my $setting = 'localhost';

if (-f '/usr/local/PerlModules/Prefs.pm') {
    do '/usr/local/PerlModules/Prefs.pm';
    my $config = Prefs->new();
    $setting = $config->read('DB_SERVER');
}

print "$setting\n";

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


Reply via email to