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

I have this script I want to use under mod-per. It has all these global variables flying around. Since mod-perl is a single instance of the interperter, these global hashes and arrays getting cluttered with data from multiple users. I tried clearing the hashes at the top of each module that uses them, but I'm thinking there is a better way. Will a module's BEGIN run if one of its methods is imported and used or just when the module is first loaded. I'm still unsure how mod-perl REALLY affects scoping. Any help appreciated. Amen. -Ty

Replies are listed 'Best First'.
Re: mod-perl and globals
by Aighearach (Initiate) on Jun 25, 2000 at 15:54 UTC
    A lot of the scoping issues can be understood by considering that mod_perl wraps you script into a subroutine, and then calls the sub each time you run the script. Sortof. ;)

    Are you sure you want to be using globals in the first place? If you are going to clear it anyway, wouldn't using a lexical be a better idea? Or a *cough*object*cough* model? If you really do need a global, you should dive into the mod_perl guide.

    I hope that hepls a little... if not, post the code and then I can add more.

    update: jjhorner stated it much better.

    Paris Sinclair    |    4a75737420416e6f74686572
    pariss@efn.org    |    205065726c204861636b6572
    I wear my Geek Code on my finger.
    
      Thanks for the reply. All of you. I agree with your suggestions. Here is the catch. I'm using an old bunch of code, about 800-900 lines, that need to be updated to work in mod-perl. It uses globals extensively and in really careless ways. This code uses a common cgi parsing routine that parses the url agrs and dumps them into globals. I want to avoid a total rewrite.

      I'm beginning to understand how mod-perl is handling things. It's different than I thought.

        Have you seen the mod_perl guide section on porting? I have found that if I am faced with 1000 lines of messy code, I can usually rewrite it from scratch in half that, with less effort than it would be to maintain the spaghetti. As St. Larry said:
        "If the burden of decision making is on the programmer, then it's possible for the programmer to make a mess of things. It's possible for Perl programmers to write messy programs. (In case you hadn't noticed.) It's also possible for Perl programmers to write extremely clean, concise, and beautiful programs."
        -- Larry Wall

        If it's ugly, replace it with something beautiful.

        Paris Sinclair    |    4a75737420416e6f74686572
        pariss@efn.org    |    205065726c204861636b6572
        I wear my Geek Code on my finger.
        
Re: mod-perl and globals
by jjhorner (Hermit) on Jun 26, 2000 at 05:35 UTC

    If I understand your question, you have issues.

    If you want to share data betweeen Apache child processes, you should use the tie() aspect of IPC::Shareable module.

    If you don't want shared information between child processes:

    For Pete's Sake, USE 'MY'!!!!

    Use 'my', use 'strict', use warnings, don't write mod_perl without them!!!

    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/
    
Re: mod-perl and globals
by reptile (Monk) on Jun 26, 2000 at 03:35 UTC

    Depends. If you're using Apache::Registry as the PerlHandler, you've got some issues to deal with that are very confusing and hard to explain. Look here to read about my() scoped variables in nested subroutines, which is essentially what file-scoped lexicals are under Apache::Registry. As Aighearach mentioned, it treats your script as if it were just another subroutine.

    Real Globals are different. If you want file-scoped variables in your script, without the other traps, then you can use vars.

    As for begin blocks, see here.

    72656B636148206C72655020726568746F6E41207473754A

Re: mod-perl and globals
by chromatic (Archbishop) on Jun 26, 2000 at 05:58 UTC
    The mod_perl solution I prefer is to write a module that houses most of your code, and make the CGI script use your module and just call init().

    Pretty simple that way.

    Disclaimer: I'm not a mod_perl wizard.