An_Idea_Within has asked for the wisdom of the Perl Monks concerning the following question:
Hello wise people!
I've been learning Perl "on the go" for a while, and I'm trying to clean up my coding style now. In some cases, I just don't know what the accepted (or a generally acceptable) "clean style" is, however. In particular, I'm trying to find a good way to work with what I consider "parameter variables", that is verbosity levels, debug settings, scripts that are to be called. Consider the following code:
#!/usr/bin/perl -w use warnings; use strict; sub processDir { my $dir = shift @_; my $script = shift @_; my $verbose = shift @_; my $ret = qx($script $dir); chomp($ret); print qq(Output:"$ret"\n) if $verbose; } # Normally via environment variable my $script = "echo -e"; # Normally via glob and processing in this script my @dirs = qw(dir1 dir2 dir3); # Normally via Getopt::Long my $verbose = 1; for my $dir (@dirs) { # Do some stuff, then: processDir($dir, $script, $verbose); }
This how I'd do it at the moment to avoid the generally much-scorned global vars. However, what is the general opinion on this? If I need the $verbose variable in nearly all functions, should I not rather call it via $main::verbose within the functions (and declare this via our in the main)? Or should I even go the use var route?
Also, the way I deal with the $script value just "feels" wrong. If I need it only in that one function, should I rather set it there? However, this doesn't feel perfectly right either, because I'd like to have all the script paramters set and viewable in one central place (the beginning of "main").
I realize there's probably no one true answer to this, but I'd appreciate thoughts and opinions of people who've coded more than just relatively simple, self-contained scripts. My aimis to myke the scripts I produce as readable and maintainable as possible for others who might come in touch with them after me. Many thanks in advance!
|
|---|