James Edward Gray II wrote:
>
> I would like to add some code to a sub that only needs to be run
> the first time the sub executes, or before is fine. If I add an
> INIT { } block at the beginning of the sub, would that do it?
>
> Are there other ways?
Nobody seems to be encouraging you to go for an INIT block. I
would have thought it was ideal, but it depends a lot on what
sort of stuff you need to do in that code, and what values it
depends on from its context - some of which may not be available
until some processing has been done.
If all you're doing is setting up run-time constants then you
should also consider using the 'constant' pragma.
Take a look at the program below to see if it might be a fit for
your problem.
HTH,
Rob
use strict;
use warnings;
use constant SYSTEM => $^O;
print "Start of program\n";
print "Operating system ", SYSTEM, "\n";
routine();
print "End of program\n";
{
my $static;
INIT { print "Initialising\n"; $static = 42 };
sub routine {
print "Running 'routine'\n";
print "Static value is ", $static, "\n";
}
}
**OUTPUT**
Initialising
Start of program
Operating system MSWin32
Running 'routine'
Static value is 42
End of program
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]