I've been tidying up a Perl CGI script I kludged together a while ago, and one of the issues I'm having is moving config info (particularly passwords) out of of the script and into a config file outside of the web directory.

The most obvious solution to this would be App::Config, however I'm still interested why the method I attempted to use does not work.

Super Search didn't dig up what I was looking for.

I was reading Tom Christiansen's Perl Style talk and came across his suggestion to use

do "/path/to/config/file.conf";
in the main script to read configuration information from another file of the form
# Config file for a perl script. $user = 'BazB' $pass = 'yaright'; $server = 'foo.bar.com';

Unfortunately, this does not seem to work under strict.

My test script is

#! /usr/bin/perl -w use strict; my ($server, $user, $pass); do "/home/baz/testconfig.conf"; print $server, "\n", $user, "\n", $pass, "\n";
However it fails with
Use of uninitialized value in print at ./readconfig.pl line 8.

I've tried playing around with the script, but the only way I can get it to 'work' is to comment out

use strict; my ($server, $user, $pass);
which is bad, bad, bad. Even then, there are warnings:
Name "main::user" used only once: possible typo at ./readconfig.pl lin +e 8.
(and similar for the other variables).

I certainly do not want to write a parser to read config files.

Why does using do in this manner fail?
Is there another simple way of reading configuration information without resorting to something like App::Config?

Cheers.

BazB

Edit: chipmunk 2002-02-10


In reply to Using do to read config by BazB

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.