in reply to Strict scripts
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:
It prints:#!/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;
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.Result: 30
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
|
|---|