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

Hi Monks !
I'm using mod_perl to write my web scripts
Few previous projected I wrote as standard CGI and used ModPerl::PerlRun to run them.
Few weeks ago I started to write a project for traffic scripts
I want to run it under ModPerl::Registry, what means that I need to write it strict.
I want to use global variable for constants like domain , sub domain , parameters from cgi etc...
It's very annoying to pass to each function the 'global' variable , is there an alternative to do this ?

the another thing I can't understand is:
script1.pl and script2.pl require the same file with some functions.
the script1.pl runs first and working fine , script2.pl gives me an error "function undefined" for one of the functions that in require file.
Why this happens?
Thanks!

Replies are listed 'Best First'.
Re: Strict scripts
by chrestomanci (Priest) on Sep 01, 2011 at 10:36 UTC

    Strict is there to protect you from silly programming errors, and to enforce good practice. Writing perl without strict is like operating a Chainsaw with the safety guards removed. It might be a bit faster but sooner or later you will come to regret it.

    My advice is to get used to programming with strict, and to avoid the use of global variables. (Which are a problem with mod_perl in any case). How hard is it to wrap your main code in a function and declare variables as local to that function?

    Just write:

    #!perl sub main { my $variable = 42; my $result = library_call($variable); # more code } # other subroutines. exit main();
Re: Strict scripts
by CountZero (Bishop) on Sep 01, 2011 at 13:23 UTC
    It's very annoying to pass to each function the 'global' variable , is there an alternative to do this ?
    You could incorporate all this information into an object and just pass the object or call different methods on the object directly.

    You could also have a look at web-frameworks like Catalyst or Dancer which have probably solved all the problems or annoyances you are now struggling with.

    the another thing I can't understand is:
    For lack of seeing the code of your scripts, the Monks will also fail to understand this problem. Please construct a minimal script that shows this error and post it here.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Strict scripts
by flexvault (Monsignor) on Sep 01, 2011 at 13:24 UTC

    First you need to realize that mod-perl and perl are approximately in sync. I just updated and tested a perl cgi script on a server without mod-perl enabled, and then had to debug it again under an enabled mod-perl server. Several functions of perl just don't work the same under mod-perl and error messages may or may not exist. But for performance, mod-perl if great.

    To handle the global problem, just keep your 'my's in scope:

    #!/usr/local/bin/perl -w { use strict; my $global = 3; my %Hash = (); my $result = Get_Result(); print "Result: $result\n"; exit; ## Optional, but I prefer to show exits. sub Get_Result { return( $global * 10 ); } } 1;
    It prints:
    Result: 30
    Notice I basically have main and all subs in brackets{}. This allows all initial definitions to be in the same scope. Works great in cgi, but you have to be careful. If you want some of the subroutines out of scope just add them after the last bracket of the initial scope. Note: I have not tested this with mod-perl, but it works for perl.

    For passing variables to subroutines, I use a hash/array in the main scope, and pass the reference to it to out-of-scope subroutines.

    my $Hash_result = Out_of_Scope( \%Hash);

    If you use one master hash, then all subroutines can be written the same way. Saves on typing if you just copy a skeleton subroutine, rename it and then add the code you need.

    Good Luck

    "Well done is better than well said." - Benjamin Franklin

Re: Strict scripts
by tospo (Hermit) on Sep 01, 2011 at 13:26 UTC
    You can use constants by declaring them with "use constant". See the perldoc