[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] weee, I get to use perl again



At Fri, 24 Aug 07, Unidentified Flying Banana Larson, Timothy E., said:
> I want to implement a simple config file by requiring a file at runtime
> - seems easy enough.  I'd like to do it smart, though, so I don't have
> to edit the path in my script when this moves into production.  There's
> got to be a simple way to get the abs path to <my_bin_dir>/../lib so the
> thing is portable.  Only a few lines to write myself, I suppose, but
> maybe even a one-liner if I just knew which function/module to use.
> I've tried File::Spec and Cwd but can't get it quite right.

I came across that same issue, and found a not too difficult way of
doing it.  There may be a better way, but this worked for me.

   use FindBin qw($Bin);
   use lib "$Bin";

   use vars qw($user $pass $url $ids_ver $max_events);
   require "ids_check.conf";

The first two lines find the directory where the binary (script) is
located, and add that directory to the library path.  The second two set
up the variables I'm going to pull, then require the .conf file.

This is my favorite way to do simple config files, because you can just
make the conf file valid perl, require/include it, and bam, you're done.
No messy config file parsing and processing. (Idea taken from
RequestTracker, though I've seen other applications use it, too.)

If you want to do full parsing and processing on a config file, you
could of course still do that, and just use the FindBin module as above
to get your current dire, then open up $Bin/../lib/foo.conf or whatever.

> Tim

-- 
| Christopher
+------------------------------------------------+
| Here I stand.  I can do no other.              |
+------------------------------------------------+