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

I have a small config file I use for a webserver setup script where I work. Here are a few representative lines:
$transfer_log = eval { "$log_dir/$user_data{site}->[0]-access_log" }; @adduser = eval { "/usr/sbin/adduser -g $group -G $user_data{site}- +>[0] -H" . "$home_dir -P $pwd -s $shell $user" };
$user and such get read in interactively at run-time, so with -w, I get warnings about using an undefined value. eval {} isn't doing what I was expecting, and it doesn't seem to matter whether I use use, require, or do {}. Any input on how to clean this up? --TQuid
  • Comment on Configuration variables using run-time in put without -w complaints?
  • Download Code

Replies are listed 'Best First'.
Re: Configuration variables using run-time in put without -w complaints?
by Shendal (Hermit) on Jun 26, 2000 at 23:56 UTC
    I am not sure why you are using eval. If I understand your issue, you want to get input from the user, set that to a few variables, and then use these variables as input to another program (in this case, adduser).

    Why not try this:
    print "Enter something:"; chomp($answer = <STDIN>); $transfer_log = "$log_dir/$answer"; $adduser = `/usr/sbin/adduser -g $group -G $user ...`;
    Anyway, I truncated a bit of the data, but I think you get the idea. You want to read in whatever your interested in. In order to run a program (i.e., adduser), use backticks or system().
    Hope that helps,
    Shendal

    Update: As KM pointed out in chat, you'll probably want to enable taint checking (-T). That will warn you on unsafe constructs where you may be running something you don't want to.
      Sorry, the eval is there as a result of recent thrashing, since I read that it is evaluated at run-time. The problem is that the snippet above is a separate config. file, read in with
      require '/etc/addvhost2rc';
      I've also tried using do {}, as suggested in the Cookbook, but the error messages persist regardless. It's important that it be a separate config file, as the script is intended to run on sites with varying directory layouts, adduser command syntax, etc.
        Oh, okay. Well, if you are doing a require to get the configuration in there, I'd suggest putting everything in the configuration file into a subroutine, then call it when you've done all the necessary set up.

        For example, your config file may look like this:
        # config file sub initConfig { $foo = "$user - bar"; # ... whatever... }
        Where your program file may look like this:
        # program require 'configuration_file'; print "Username:"; chomp($user = <STDIN>); # now $user exists, although you'd wanna check it for validity &initConfig(); # ... continue processing...
        Does that solve your problem? I've done it this way in the past, and it has worked out well.
        Hope that helps!
Re: Configuration variables using run-time in put without -w complaints?
by Adam (Vicar) on Jun 26, 2000 at 23:58 UTC
    When you say the $user is defined at run-time, I assume you mean before you get to these lines. So that would (theoretically) mean that you shouldn't be getting the undef warning. Prove this to yourself using a more defensive programming style. Ie
    if( defined $log_dir and exists $user_data{site} ) { $transfer_log = eval { "$log_dir/$user_data{site}->[0]-access_log" + }; } else { print STDERR "log_dir was undef or site wasn't in user_data\n"; + }
    My guess is that the vars arn't really defined. Maybe you have them in a loop or something where the last case gets run with undefs.