>>>>> "James" == James Edward Gray <[EMAIL PROTECTED]> writes:

James> I would like to add some code to a sub that only needs to be run the
James> first time the sub executes, or before is fine.  If I add an INIT { }
James> block at the beginning of the sub, would that do it?  Are there other
James> ways?  Thanks.

In this code, the two variables are initialized at compile time,
and then the subroutine can access them.

    BEGIN {
      my $static_local_1;
      my $static_local_2;

      ## initialization:
      if (rand > 0.5) {
        $static_local_1 = 35;
        $static_local_2 = 19;
      } else {
        $static_local_1 = int rand(50) + 3;
        $static_local_2 = 8;
      }

      sub mangle_some_data {
        return $_[0] * $static_local_1 + $_[1] * $static_local_2;
      }
    }

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to