#!/usr/bin/perl #use 5.010; ############# I use this in the larger script , just not here use strict; use warnings; my $debug = $ENV{"DEBUG"} // 1; # Debug set as default while testing my (%config, $key); &load_config ("./backup.conf"); # Location hardcoded for testing only while ( (my $key, my $value) = each %config ) { print "$key => $value\n"; } # Set parameters print "Setting parameters\n" if $debug; my @keys = ('RSYNC_OPTIONS', "RSYNC_OPTIONS"); #unshift @keys, RSYNC_OPTIONS; foreach (@keys) { $key = $_; print "$key\n"; # print "Exists\n" if exists $config{$key}; if (exists $config{$key}) { print "Exists\n"; print "Defined\n" if defined $config{$key}; print "True\n" if $config{$key}; print $config{$key} . "\n"; } else { print "$key does not exist\n"; } } print "No quotes:"; print $config{RSYNC_OPTIONS}; print "\nSingle quotes:"; print $config{'RSYNC_OPTIONS'}; print "\nDouble quotes:"; print $config{"RSYNC_OPTIONS"}; print "\n"; sub load_config { my $file = shift @_; print "Loading configuration file, $file\n" if $debug; my $opened = open CONFIG, "<", $file if (-e $file); if ($opened) { print "Opened configuration file, starting to process\n" if $debug; while () { chomp; # Skip lines not containing a key-value pair s/#.*//; # no comments s/^\s+//; # no leading white s/\s+$//; # no trailing white next unless length; # anything left? # Split the line into the key and the value (my $key, my @value) = split /=/, $_; # my $temp = q/$key/; # print "$temp\n"; # $key = q/$temp/; my $value = join '=', @value; # $temp = q/$value/; # print "$temp\n"; # $value = q/$temp/; print "Retrieved key-value pair: $key => $value\n" if $debug; $config{$key} = $value; print "$key holds $config{$key}\n" if $debug; } print "Finished processing, closing configuration file\n" if $debug; close CONFIG; } else { warn "Can not open the configuration file: $file (Reported error: $!)"; } $opened; }