ITworld.com
  Search  
ITworld Home Page ITworld Webcasts ITworld White Papers ITworld Newsletters ITworld News ITworld Topics Careers ITworld Voices ITwhirled Changing the way you view IT
Requiring Configuration Data
PERL --- 08/02/2001

Andrew Johnson

Often we write programs that need to use configuration data supplied by the user, but we do not wish the user to have to supply that information on the command line every time they run it. In such circumstances, allowing the user to store the information somewhere and have the program utilize it on each invocation can be useful. 

On this topic

One simple way to do this is to have the user put the data after the __DATA__ in the script and have the script read it in:

#!/usr/bin/perl -w
use strict; my %config = configure();

foreach my $opt (keys %config) {
print "$opt : $config{$opt}\n"; }

sub configure {
my %cfg; while(<DATA>){ next if /^\s*#/ or /^\s*$/; chomp; my($option, $value) = split /\s*=\s*/; $cfg{$option} = $value; } return %cfg; }

__DATA__
# This is the configuration section. Syntax is: # OPTION_NAME = VALUE

# set user name:
USER_NAME = nobody

# set email address
EMAIL = nobody@nowhere.com

# debugging level (1,2,3)
DEBUG = 2

The pitfalls of this approach are twofold: first, only one user of the script may configure it (each user must run their own copy of the program with their own configuration details), and second, we are responsible for parsing the configuration info.

We can get around the second pitfall by asking the user to use Perl's hash syntax and set their configuration info in the hash at the top of the script:

#!/usr/bin/perl -w
use strict; my %config = ( # set user name: USER_NAME => 'nobody',

# set email address:
EMAIL => 'nobody@nowhere.com',

# set debugging level (1,2,3):
DEBUG => 2, );

However, this is still set within the script and we want to allow for different users of the same program. To do this we use a separate config file for each user (stored in the users home directory with a special name). We can choose to use the same form as the __DATA__ example above and write our own parser, or we can specify a format that Perl can understand. One trick for having Perl do the work is to realize that Perl's require() function (used to load in modules or other chunks of Perl code) has a return value of the last expression evaluated in the file. This means we can use an anonymous hash as our configuration format and have it be the only thing in the file -- for example, a config file could look like:

{
# set user name: USER_NAME => 'nobody',

# set email address:
EMAIL => 'nobody@nowhere.com',

# set debugging level (1,2,3):
DEBUG => 2, }

As far as Perl is concerned, that is an anonymous hash and if this file is stored as '.config' in the users HOME directory (where the environment variable $HOME points to), we can simply require() it and grab the return value:

#!/usr/bin/perl -w
use strict; my $config = require "$ENV{HOME}/.config"; foreach my $opt (keys %$config) { print "$opt : $config->{$opt}\n"; }

Now each user can have their own config file and we avoid having to parse it by using a format (an anonymous hash) that Perl understands. Utilizing the return value of the require() function can be useful in other situations as well.

Next Week: Tying Variables

 

Andrew Johnson works as a programmer/consultant and is the author of Elements of Programming with Perl from Manning Publications.



Advertisements
Sponsored links
Locate Hidden Software on business PCs with this free tool
Top 5 Reasons to Combine App Performance and Security
KODAK i1400 Series Scanners stand up to the challenge
Bring harmony to your mix of UNIX-Linux-Windows computing environments
 Home   Newsletters  PERL
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Industry Standard   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.