Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Yet another config file editing programme : Tell me how to make it better !

by eyepopslikeamosquito (Archbishop)
on Sep 02, 2021 at 13:37 UTC ( [id://11136368]=note: print w/replies, xml ) Need Help??


in reply to Yet another config file editing programme : Tell me how to make it better !

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

  • No subroutines!
  • Poor scoping of variables: $ip_line, for example, is a glaring example of an evil global variable.

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:

  • Minimize the scope of variables, pragmas, etc.
  • Think about how to test your code from the beginning. This improves code quality because: writing a test first forces you to focus on interface (from the point of view of the user); hard to test code is often hard to use; simpler interfaces are easier to test; functions that are encapsulated and easy to test are easy to reuse; components that are easy to mock are usually more flexible/extensible; testing components in isolation ensures they can be understood in isolation and promotes low coupling/high cohesion. See also: Effective Automated Testing and Test::More.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11136368]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-24 06:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found