mattg has asked for the wisdom of the Perl Monks concerning the following question:

Hello all!

I am in need of some wisdom. I am working on what is becoming an ever more enormous script (about 4000 lines now and still growing, but mostly because it employs the Tk module to build a gui to a database), and beginning to worry about the way that I'm passing variables to the various subroutines.

Until writing this script, I had always passed variables to subroutines via @_ (and had blindly assumed that was the only way), but this script requires access to a number of pseudo-constants (values that are not constant, but the user is limited to only certain selections). Consequently, after a little investigation, I discovered that you could access globally scoped 'my' variables from within subroutines, so rather than needlessly keep passing the same pseudo-constants to each subroutine, I just started accessing them as I would within any other block of code.

Now that I'm nearing the end of this phase of the project, I'm starting to look at the code for any missed bugs or obvious failures, and this (what seems to me now possibly reckless) use of variables has me worried.

So I guess my question is, does anyone see any obvious failures in this approach? I realize that passing variables by reference will prevent accidental modifications of the original, but these values are never modified in the subroutines (i.e., they are used to specify which database to access, etc.). Anyone want to pat me on the head and reassure that I haven't committed a heinous coding crime?

Thanks in advance,

matt

  • Comment on Passing variables is as easy as passing gas

Replies are listed 'Best First'.
Re: Passing variables is as easy as passing gas
by dragonchild (Archbishop) on Nov 02, 2001 at 23:14 UTC
    Here's a thought - why not put those semi-constant values in some external module that inherits from Exporter. You can then write a number of "accessor" functions to those values, and import those into your script. Something along the lines of:
    use 5.6.0; use strict; package MyConstants; use Exporter; our @ISA = qw(Exporter}; our @EXPORT_OK = qw(Foo Bar); my $foo = 5; sub Foo { $_[0] ? $foo = $_[0] : $foo } my $bar = 3; sub Bar { $_[0] ? $bar = $_[0] : $bar } 1; __END__
    Thus, you can get and set these values. This, in case you're wondering, if a very rudimentary datastructure object.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

(jeffa) Re: Passing variables is as easy as passing gas
by jeffa (Bishop) on Nov 03, 2001 at 00:00 UTC
    Code that is 1000+ lines and is not OO probably should be OO. Do what dragonchild says, or read this book: Object Oriented Perl and learn how to decompose your logic into a set of maintable, understandable, and hopefully readable classes. Hell, do both!

    Global variables are not a BAD thing - but they can be easily abused about 1000 lines into your code. By encapsulating these variables into proper classes, you will hopefully achieve better code re-use. I say hopefully because first attempts at OO are usually not pretty. :)

    jeffa

      Okay, I'll show my utter ignorance of OO and ask if OO in Perl is anything more (in a general sense, of course) than modularized subroutines? Aside from the global variables, I've broken the script up into numerous subroutines all of which operate (as much as is possible) independent of each other.

      Also, of the 4000 lines, only about 750 are actually in the main loop of the Tk interface, the rest are taken up by the subroutines (21 in total). It's for this reason that the global variables worry me, because after a while it does become difficult to know where they have been littered throughout the subroutines and ensure that I haven't accidentally put them in a position of being modified (but I'm checking my code with a fine-toothed comb now, as the saying goes).

      I'm going to see if I can get my hands on a copy of the OO Perl book, and will see what I can do about putting the globals into a module. Thanks for the responses!

      matt