This article details how to read parameters from an INI file in a Perl script.
DL» read_ini.zip
Reading Parameters from an INI File
When developing scripts, quite often it is desirable to use the same script for multiple instances.
Especially when the same purpose is being performed and only a few settings are different. Storing
settings in an INI is definitely not a new concept but often it is still applicable. I've found it
most flexible to have the script take a single command-line parameter...the INI file. Each INI file
contains the specific settings for each instance like machine-specific information, for example.
The sample code presented here is a continuation of the "Reading Command-line Parameters" code.
Where this code is different than others I found when I first started is the order of parameters stored
in the INI file does not matter. I use a simple Regular Expression against each line read and extract the
setting to be stored in an anonymous hash. The other technique employed here is only the first setting found
is stored, in the event multiple instances of the same parameter are found in the same INI file. It's a little
safe-guard against inadvertent duplication or tampering of settings.
Copyright© 2004 Michael McLean
#
# Usage: perl.exe read_ini.pl --infile=<ini_text_file>
# --infile Text file INI settings.
#
# Last Updated [08 Nov 2004]
use strict;
use constant SUCCESS => 1 ;
use constant ERROR => -1 ;
use constant INVALID_SYNTAX => -2 ;
# Anonymous hash reference to store INI settings.
my $ini = { ipaddr => '',
owner => '',
os => '',
};
# Parameter count check.
if( scalar( @ARGV ) != 1 )
{
print "Invalid syntax!\n\n";
print "*** Application syntax:\n";
print " read_ini.pl --infile=<ini_text_file>\n";
exit( INVALID_SYNTAX );
}
my $infile = $ARGV[0];
# Remove --infile= from command-line parameter.
# Leaves only file name.
if( $infile =~ /^--infile\=(.*)/i )
{
$infile = $1;
$infile =~ s/^\s+//;
}
else
{
print "Invalid syntax!\n\n";
print "*** Application syntax:\n";
print " read_ini.pl --infile=<ini_text_file>\n";
exit( INVALID_SYNTAX );
}
# Check length of file name.
# File name MUST be > 2 and < 256 characters in length.
if( length( $infile ) > 255 || length( $infile ) < 3 )
{
print "Invalid --infile value!\n\n";
print "File name length MUST be > 3 && < 255\n";
print " Length = " . length( $infile );
exit( INVALID_SYNTAX );
}
# Open file in READ mode.
open( my $fh, "<$infile" ) or die( "INPUT ERROR: Error opening $infile\n$!\n$^E" );
# Booleans to indicate if INI parameters have been read.
my( $bip, $bowner, $bos ) = 0;
# Process infile
while( defined( my $entry = <$fh> ))
{
# Remove CRLF from line.
$entry =~ s/[\x0A\x0D]//;
# Check to see if line contains IP address setting
# and the setting has not already been saved in the hash.
if(( $entry =~ m/^ipaddr\=(.*)/i ) && ( $bip == 0 ))
{
$ini->{ipaddr} = $1;
$ini->{ipaddr} =~ s/^\s+//;
$ini->{ipaddr} =~ s/\s+$//;
$bip++;
}
# Check to see if line contains Owner setting and the
# setting has not already been saved in the hash.
elsif(( $entry =~ m/^owner\=(.*)/i ) && ( $bowner == 0 ))
{
$ini->{owner} = $1;
$ini->{owner} =~ s/^\s+//;
$ini->{owner} =~ s/\s+$//;
$bowner++;
}
# Check to see if line contains OS setting and the
# setting has not already been saved in the hash.
elsif(( $entry =~ m/^os\=(.*)/i ) && ( $bos == 0 ))
{
$ini->{os} = $1;
$ini->{os} =~ s/^\s+//;
$ini->{os} =~ s/\s+$//;
$bos++;
}
}
# Close file handle.
close( $fh );
if(( $ini->{ipaddr} ne '' ) && ( $ini->{owner} ne '' ) && ( $ini->{os} ne '' ))
{
print $ini->{ipaddr} . "\n";
print $ini->{owner} . "\n";
print $ini->{os} . "\n";
}
else
{
print "FAIL :: Missing INI settings!";
exit( ERROR );
}
