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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |