Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Config File Placement

by skazat (Chaplain)
on Apr 29, 2002 at 02:17 UTC ( [id://162742]=perlquestion: print w/replies, xml ) Need Help??

skazat has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I have an interesting scenario,

I have a group of programs that reads in a config file. These programs can all be called differently; some are cgi scripts, some are executed when a email is piped to them, some are called via a cron job. The config file itself is read in by a module, we'll say ProgConfig.pm On top of scripts that use ProgConfig.pm are two lines similar to this:

 
use lib '/home/account/privateperllib/';
use ProgConfig.pm; 

ProgConfig.pm holds defaults that can be changed via the config file it reads, which, at the moment, is just evaled in. This allows easy updating of the program's libraries, without having to tweak the ProgConfig module itself. The problem is, depending on what enviroment ProgConfig is called from, finding the config file can be a bit difficult.

some my question is, what would be a good place to put a config file that the ProgConfig module can always find?

I was thinking, (duh) the home directory of the user would bea good spot to put the config file. This is usually held in $ENV{HOME} but! this Env variable sometimes isn't available to cgi scripts. The closest variable I can find is $ENV{DOCUMENT_ROOT} which, isn't the home dir, it's usually the public html dir. I could probably munge the $ENV{DOCUMENT_ROOT} variable and whack off the last directory and say 'this is the home directoy' but that doesn't seem like the best idea.

Has anyone solved something similar to this?

 

-justin simoni
!skazat!

Replies are listed 'Best First'.
Re: Config File Placement
by Kanji (Parson) on Apr 29, 2002 at 04:06 UTC

    If your CGIs run as the user who created them instead of the webserver (ie, setuid through something like Apache's suEXEC), then you can fallback to getpwuid if $ENV{'HOME'} is unset.

    my $HOME = $ENV{'HOME'} || ( getpwuid $> )[7] or die "Uh oh, I've been evicted...\n";

    If you're feeling particularly dangerous, you could also try falling back to the current directory (|| ".") if neither $ENV{'HOME'} or getpwuid return something useful, with the caveat being the current directory may not always be what you expect it to be.

    However, I find such a solution limiting: what if you want to run the same app with different configurations? Not possible unless you subvert $ENV{'HOME'}.

    A better way might be to have each of your apps explicity set where their config directory (or better yet, the config file itself) should be, and then optionally have ProgConfig.pm fallback to the $ENV{'HOME'}-style above if they didn't.

    Something like...

    package ProgConfig; sub import { my $package = shift; my $config_dir = shift || $ENV{'HOME'} || ( getpwuid $> )[7] or die "Shazbot! Where's my config dir at?\n"; # do whatever to load config } 1;

    Which you'd then call from your apps as...

    use ProgConfig '/path/to/my/config_dir';

        --k.


      this is good stuff to chew on...

      Shouldn't this line:

      my $config_dir = shift || $ENV{'HOME'} || ( getpwuid $> )[7]

      be changed to:

      my $config_dir = $_[1] || $ENV{'HOME'} || ( getpwuid $> )[7]

      $_[0] seems to return the namespace of the module, or if that ain't the right term (like I know what I'm talking about) it would return something like

      ProgConfig
      </code>

       

      -justin simoni
      !skazat!

Re: Config File Placement
by belg4mit (Prior) on Apr 29, 2002 at 03:38 UTC
    You might find AppConfig and Config quite enlightening.

    As for $ENV{HOME}, if this is your server and you happen to be using Apache you might use SetEnv or PassEnv. Another alternative is to use getpwent(UPDATE: pwuid being the better of the pw* choices) to find the HOME of the current user.

    --
    perl -pew "s/\b;([mnst])/'$1/g"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://162742]
Approved by rob_au
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 21:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found