# Reads a text file with var=values pairs line delimited # Storing the values into a hash filled array. sub readDat { # Grab the passed parameter my $filename = $_[0]; my $state = $_[1]; # Establish the array to hold the aquired Data # and pass back to calling Function my @Data; # The ubiquitous $i counter my $i = 0; debug( "Reading dat file - $filename\n", 5 ); # Register a filehandle local (*DATFILE); # Open the file for read open (DATFILE, $filename) or debug( "Can't open $filename: $1", 100); # Read through the file one line at a time FORA:while () { debug("Proccessing Line = \n$_\n", 0); # Skip over any comments if ( /#.*/ ) { next FORA; } # Clean up any extraneous garbage chomp; # no newline s/^\s+//; # no leading white s/\s+$//; # no trailing white # If clean up eliminated any data worth reading # lets skip to the next line next unless length; debug( "Loading Line = \n$_\n", 0 ); # We can't load the lines with $VARS as we will # loose any values so we'll make sure to escape them # (the $'s that is) s/\$/\\\$/g; # localizing $var and $value to make sure # they are clean out on every read. my ($var, $value) = split(/=/,$_); if ( $var && $value ) { debug( "\$var = $var \$value = $value\n", 0 ); } # Stuff those puppies into a hash and store # the hash into our @Data array # (See Camel 3rd ed pgs 278 - 279) $Data[$i]{$var} = $value; # Increment and get the next $i++; } # Send the info back to the caller return @Data; }