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


In reply to Re: Strict scripts by flexvault
in thread Strict scripts by AlfaProject

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.