First, I'm new to perl and am still working through Learning Perl 5th ed., in chapter 11. However, I've used both VBA and VB.Script a lot and some VB.Net so I'm not new to programming entirely.

Overview

I'm writing a simple backup program (backup.pl) using rsync to a home server running ubuntu jaunty. I have a configuration file (backup.conf) that stores the details for rsync. I wrote my own code, sub load_config, to process the configuration file. (NOTE: I did this in lieu of using a CPAN module since I'm using cygwin on Win XP and could not get CPAN modules to install.) The sub processes the config file and uses the hash declared in the main procedure to store the keys and values.

Problem

Within the load_config sub I can retrieve the value for a key and print the entire hash out iteratively. Within the main procedure I can print the entire hash out iteratively but CAN NOT retrieve the value for a key. No matter how I try to use the key (no quotes, single or double quotes, assigning to a variable first) I'm warned about an 'unitialized value' and get nothing but an empty string.

Efforts

I've tried googling and PerlMonks' super search but probably couldn't find the answer because of poor search terms. I found several tutorials and examples. Some of these use a bareword key within the braces, { }, others use double quotes, and a few use single quotes, providing I'm reading them correctly. I think it must be a syntax issue since I can iterate over the hash within the scope fo the main procedure, just not retireve the value using the key, both of which I can do within the subprocedure. I'm just puzzled and no doubt my lack of perl knowledge is hampering my ability to find the solution. I was tempted to throw my laptop around last night--way too tired and frustrated. I'd appreciate any help that points me in the right direction. The actual code and config data follow.

Files

Script file -- backup.pl

This is an excerpt but contains all the code at issue

#!/usr/bin/perl #use 5.010; ############# I use this in the larger script , just not h +ere use strict; use warnings; my $debug = $ENV{"DEBUG"} // 1; # Debug set as default while te +sting 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 $d +ebug; while (<CONFIG>) { 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 $deb +ug; $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 err +or: $!)"; } $opened; }
Config file -- backup.conf
RSYNC_OPTIONS = -avvyz --delete RSYNC_RSH = ssh LOG-FILE = backup.log EXCLUDE-FROM = backup.exclude INCLUDE-FROM = backup.include

In reply to Retrieve value with key warns 'unitialized value' although present by strawberrysocialist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.