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!


In reply to Best Practice: How do I pass option parameters to functions? by An_Idea_Within

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.