previous | top | next

Who, What, Where, When, How, and Why... But Mostly What and Why.


Descriptive Comments Make Code More Understandable

#!/usr/bin/perl

package HashFromFile;

# Used for testing and debugging
use Object;

sub make {
    my ($class, %args) = @_;

    # The text file that provides the input for the hash we're going to create
    my $file = $args{filename};

    # Init the hash reference that will be populated with data read from $file
    my $hash = {};

    # Open readonly
    open(FH, "< $file");
    
    while (my $line = <FH>) {
        # Skip over whitespace only lines
        if ($line =~ /^\s+$/) {
            next;
        }
        chomp($line);

        # Split each line into a list by the ':' character, only the words
        # around the first encountered ':' are used 
        my ($key, $value) = split(/:/, $line);

        $hash->{$key} = $value;
    }

    close(FH);
    
    return $hash;
}

1;


Email: bschmaus@combinenet.com