What slapped me in the face the instant I saw your script is:

While this style of programming may work for small throw away scripts, it does not scale over time. As your program suite grows over time, you'll want to put utility subroutines into modules, along with unit tests, so the code can be understood in isolation, tested in isolation, and reused by multiple scripts. For that to work, you must avoid global variables. Instead, each subroutine must have well-defined inputs and outputs.

To illustrate, here's a sample subroutine to read your input file along with an illustrative trivial main program that calls it:

use strict; use warnings; use Data::Dumper; # Read an input file # Return a reference to a hash of properties sub get_properties { my $fname = shift; open my $fh, '<', $fname or die "open '$fname': $!"; my %hash_ret; my $line; while ( defined( $line = <$fh> ) ) { chomp $line; $line =~ s/^\s+//; # remove leading $line =~ s/\s+$//; # and trailing whitespace next unless length $line; # ignore empty lines next if $line =~ /^#/; # ignore comment lines my ($key, $val) = split /\s*=\s*/, $line, 2; $hash_ret{$key} = $val; } close $fh; return \%hash_ret; } @ARGV or die "usage: $0 property-file\n"; my $prop_file = shift; print "prop_file='$prop_file'\n"; my $properties = get_properties($prop_file); print Dumper( $properties );

Note that this subroutine does not use any global variables, just reads the specified input file and returns a reference to a hash of properties ... so could be easily put into a module and reused by many different main programs in your suite.

A couple of related points from On Coding Standards and Code Reviews:


In reply to Re: Yet another config file editing programme : Tell me how to make it better ! by eyepopslikeamosquito
in thread Yet another config file editing programme : Tell me how to make it better ! by dazz

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.