« UCMWB su bug | Main | comptree/libs »

ucmwb 1.2.3 release/Perl'isms

  • Released ucmwb 1.2.3

FindBin, Getopt::Long and Display.pm

Here's how I typically use FindBin:

use FindBin;
...
use lib "$FindBin::Bin/../lib";
...
sub Usage ($) {
  my ($msg) = @_;

  display "ERROR: $msg\n" if defined $msg;
  display <<END;
Usage: $FindBin::Script [-usage] [-verbose] [-debug] [-exec]
		-from_view <view> -label <label_name>
...
END

Also I tend to use Getopt::Long. It is extremely flexible. Options can be specified in the GNU long method as in --debug or just -debug. Additionally you can abbreviate as in -d IFF there is no other option that starts with "d". Options can have parameters (-file myfile.txt or -file=myfile.txt), set boolean (-verbose can set say $verbose), automatic negation (-verbose or -noverbose), even call subroutines directly!k Here's an example.

use GetOpt::Long;
...
my $execute	= 0;
my $from_view;	# e.g. p6258c_RAN_FDD_Doc_Bld1_int
my $branch;	# e.g. ran_fdd_release_build2_integration
my $label;	# e.g. RAN_FDD_RELEASE_BUILD1

GetOptions (
  "usage"	=> sub { Usage "" },
  "verbose"	=> sub { set_verbose },
  "debug"	=> sub { set_debug },
  "exec!"	=> \$execute,
  "from_view=s"	=> \$from_view,
  "branch=s"	=> \$branch,
  "label=s"	=> \$label,
) || Usage "Unknown parameter";

Some notes about the above:

  • Usage is a subroutine (most people have one) that takes one parameter for an extra message to display first.
  • Eat your own dog food! I use my Display module which has display routines for verbose and debug, etc. It also has $verbose and $debug variables and exports the set_verbose and set_debug methods.
  • The "exec!" is a negation. So -exec and -noexec apply and set $execute accordingly
  • All of "from_view", "branch" and "label" are simple name/value type parameters so -branch <branchname> sets $branch to <branchname>.

A word on Display.pm:

I find that often people do the following:

print "In myspecial_subroutine: File = $file\n" if $debug;

for debugging. To me it's always been a hassle to have to remember to include the "\b" and the "if $debug", etc. Granted that's not a lot. But how can I make it better?

My thought was to name the procedure debug and include the "\n" by default. People oughta quickly discern that:

debug "In myspecial_subroutine: File = $file";

probably only happens when debugging.

Additionally I support routines like verbose, debug, error, warning (from Display.pm).

TrackBack

TrackBack URL for this entry:
http://defaria.com/mt/mt-tb.cgi/64