Professional Web Applications Themes

Questions regarding use: "optional" modules, and "refreshing" modules - PERL Beginners

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; ...

  1. #1

    Default Questions regarding use: "optional" modules, and "refreshing" modules

    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?

    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
    Ed Guest

  2. #2

    Default Re: Questions regarding use: "optional" modules, and "refreshing" modules

    > Folks, 

    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.
     

    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 Guest

  3. #3

    Default RE: Questions regarding use: "optional" modules, and "refreshing" modules

    Wiggins d Anconia wrote: 
    >
    > 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.
    >  
    >
    > 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[/ref]

    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";
    Ed Guest

Similar Threads

  1. Replies: 1
    Last Post: April 24th, 01:27 PM
  2. CFINPUT type="radio" w/ "value" requires "label"
    By Iceborer in forum Macromedia ColdFusion
    Replies: 2
    Last Post: February 21st, 06:16 PM
  3. "Start" "Program" "Menu" list is empty
    By Pete in forum Windows XP/2000/ME
    Replies: 2
    Last Post: July 10th, 10:42 PM
  4. [Newbie] "failed ro read /modules/mosules-info"
    By St Bernard in forum Linux Setup, Configuration & Administration
    Replies: 4
    Last Post: June 28th, 10:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139