I am relatively new to Perl and would like to check that the code I am writing docent have any major flaws in it or any bad practices.
This is a fairly simple sub but one that gets used often. Seems to work OK. Simply reads a text file that has parameters in it set by the user.
Config.txt
#cucm username and password of user associated with phones
username = bob
password = bobPass
#subnet details. Use /30 to start with to test 2 phones before entire subnet
subnet = 10.64.97.216/32
#HFS web server details IP and folder
webserver = http://10.64.164.230/Desktop/
#image file names. Full and Thumbnail size
imgFullSize = screen_F.png
imgThumSize = screen_S.png
Perl sub that reads the file:
sub readConfigFile
{
print $lfh "Reading Config.txt file\n";
#path to the config file
my $configF = "config.txt";
if($DEBUG == 1){print "opening config File\n";}
open(my $configFhandle, '<', $configF) or die "Unable to open file, $!";
my @arrFileData=<$configFhandle>; #Slurp!
#go through each of the lines in the file
for (my $i = 0; $i <= $#arrFileData; ++$i)
{
local $_ = $arrFileData[$i];
if($DEBUG == 1){print "Reading line:$_";}
#check to see if the line is a comment... if it is skip it
if($_ =~ /^#/)
{
if($DEBUG == 1){print "Line is comment. Skiping:$_";}
next;
}
#split the line into the key and value
my @dataKV = split /=/, $_;
#check to see that Key and Value exist ... if not skip it
my $arrSize = @dataKV;
if($arrSize <= 1)
{
if($DEBUG == 1){print "Read Config: Error: No Key AND Value found:$_";}
next;
}
#remove all leading and end white spaces around elements
s{^\s+|\s+$}{}g foreach @dataKV;
for (my $x = 0; $x <= $#dataKV; $x++)
{
if($DEBUG == 1){print "Key:$dataKV[$x]\n";}
given($dataKV[$x])
{
when (/^username/)
{
$usr = $dataKV[++$x];
if($DEBUG == 1){print "Value Username:$dataKV[$x]\n";}
next;
}
when (/^password/)
{
$pass = $dataKV[++$x];
if($DEBUG == 1){print "Value password:$dataKV[$x]\n";}
next;
}
when (/^subnet/)
{
@arrSubnets = $dataKV[++$x];
if($DEBUG == 1){print "Value Subnet:$dataKV[$x]\n";}
next;
}
when (/^webserver/)
{
$webserver = $dataKV[++$x];
if($DEBUG == 1){print "Value webserver:$dataKV[$x]\n";}
next;
}
when (/^imgFullSize/)
{
$imgFull = $dataKV[++$x];
if($DEBUG == 1){print "Value imgFullSize:$dataKV[$x]\n";}
next
}
when (/^imgThumSize/)
{
$imgThum = $dataKV[++$x];
if($DEBUG == 1){print "Value imgThumSize:$dataKV[$x]\n";}
next
}
}#end of switch statement
}#end of for loop going through the key and value line
}#end of For loop going though the lines in the config
print "*********************************\n";
print "*Config imported\n";
print "*********************************\n";
print "*username :$usr\n";
print "*password :$pass\n";
print "*subnet :@arrSubnets\n";
print "*webserver :$webserver\n";
print "*imgFullSize :$imgFull\n";
print "*imgThumSize :$imgThum\n";
print "*********************************\n";
print "Is this above correct?[Y]:";
my $confirm = <>;
if($confirm !~ /^Y/)
{
print $lfh "Config file needs to be changed. Closing script down.\n";
close($lfh) or warn "Unable to close the file handle: $!";
close($configFhandle) or warn "Unable to close the file handle: $!";
print "Script will now terminate. Please amend \"Config.txt\" file and re-run script. Press any key to terminate";
<>;
exit;
}
#close the file
close($configFhandle) or warn "Unable to close the file handle: $!";
print $lfh "Y pressed by User. Config all good.\n";
}
Any advise on how to improve this would be great.
Config::Tiny
or another module for reading ini files. \$\endgroup\$