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

I'm writing a large web application (http://www.annotateit.com) which does some initialization at the head of every script. At the moment, each bit of initialization is custom to each script, but I'd like to change that so that the scripts are a little bit more maintainable. What is the best way to do that?

Replies are listed 'Best First'.
Re: Code Initialization
by mattriff (Chaplain) on Jul 28, 2003 at 15:30 UTC
    The snap answer is to abstract things out into a module that can be used by all the scripts.

    Could you give some more details, and perhaps a code sample? It's not clear what you are asking for, so it's hard to give a better answer.

    - Matt Riffle

      I was thinking along the same lines. See below.
Re: Code Initialization
by sauoq (Abbot) on Jul 28, 2003 at 15:50 UTC

    It sounds like you want to use config files. There are plenty of modules that might help you with that such as AppConfig and Config::General.

    -sauoq
    "My two cents aren't worth a dime.";
    

      I use Config::Simple to get some values out of a text file, but other values (like whether a user has authenticated, what their ID is, etc.) have to come from the user. I was thinking of wrapping everything in a Module, but I'm not sure the best way to do it. Ideally, the solution would be upwardly mobile to mod_perl so that I wouldn't have to do any more research to figure out how to port it to mod_perl. I've been thinking that perhaps a do <filename> would work, but I'm concerned about namespace issues, and I'm not sure if I would get new values with a do and mod_perl. (I've not yet started the process of porting to mod_perl, so this requirement is a phantom at the moment... not really important, just a wish).

      Here is a sample of the configuration code at the top of one file. Ideally this kind of stuff would go somewhere central and be pulled by all scripts in the suite:

      my @chars = ('A'..'Z','a'..'z',0..9); my $config = Config::Simple->new("../etc/annie.conf"); my $dbh = &widgets::dbConnect($config); my $authInfo = &auth::authenticated($dbh); my $scriptdir = $config->param("server.scriptdirectory"); my $serverURL = $config->param("server.url"); my $docURL = $config->param("server.documenturl"); my $action = $http::C->param("action") || ""; my $docDir = $config->param("server.documentdirectory"); my $ID = $http::C->param("ID"); my $template = Template->new( RELATIVE => 1, INCLUDE_PATH => "../templates");
Re: Code Initialization
by Anonymous Monk on Jul 28, 2003 at 16:18 UTC
    For CGI I would use CGI::Application. It allows you to use one file to hold all your "run modes" and then to break it down from there.
Re: Code Initialization
by liz (Monsignor) on Jul 28, 2003 at 15:30 UTC
    You're not giving us much information there. You mean you would like to maintain the customized bits of scripts centrally?

    Since you seem to be using PHP, are you sure you're at the right place here?   ;-)

    HTTP/1.1 200 OK
    Date: Mon, 28 Jul 2003 15:38:50 GMT
    Server: Apache/1.3.27 (Unix) PHP/4.3.2
    Transfer-Encoding: chunked
    Content-Type: text/html
    
    Liz

    Update: added smiley, lighten up folks!

      Since you seem to be using PHP, are you sure you're at the right place here?

      So his server has mod_php loaded... So what?! Haven't you ever written a CGI script and run it on a server with PHP installed? How do you know it's even his decision and not his hosting provider's?

      It's one thing to have a personal dislike of PHP. It's quite another to ask someone "are you sure you're at the right place" just because PHP is installed on the server he is using. That's snobbery at its worst.

      liz --

      -sauoq
      "My two cents aren't worth a dime.";
      
        ...So his server has mod_php loaded... So what?! Haven't you ever written a CGI script and run it on a server with PHP installed?

        Actually, no. I have used Perl to generate PHP source code, to reduce the load on a MySQL database, if you must know. ;-) But that was way back in 1995.

        How do you know it's even his decision and not his hosting provider's?

        That is a good point. I've never been in the situation of being dependent of an ISP in that way, so I did not consider that. I should have.

        ...It's one thing to have a personal dislike of PHP...

        I don't have a personal dislike of PHP. Have used it for years (from 1995 to 1999). I wasn't happy with PHP in 1996 (mainly for reliability reasons) and developed my own templating system. Had PHP been more reliable then, I would never have done that, and for all we know, might still have been a PHP user to this very day.

        And even if I would dislike PHP, I would have a right to it, since "I've been there and I've done that". Not like snobs who disrespect anything they do not know.

        Liz

        A reply falls below the community's threshold of quality. You may see it by logging in.
      We have several hosts on our server that do use PHP. I'm not using it for the annotateit.com application, though. I'm a perl guy.
        I'm glad to hear.

        I hope the other replies have been of use to you.

        Liz