James Edward Gray II <[EMAIL PROTECTED]> 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?

    No. Not in a subroutine.

: Are there other ways?  Thanks.

    You could use a Persistent Private Variable. It is
declared outside your sub and is only visible to your
sub.


use strict;
use warnings;

print foo();
print foo();
print foo();

{
    # the value of $ran_once will be incremented each time
    # the sub runs. Perl will keep the new value between
    # calls to the sub. $ran_once is only available to the
    # sub
    my $ran_once = 0;

    sub foo {

        # This is the return for subsequent calls
        return "foo\n" if $ran_once++;

        # this is the return of the first call to foo()
        return "First time!\n";
    }
}

__END__


    You can read more on this topic in 'perlsub' in the
"Persistent Private Variables" section.


HTH,

Charles K. Clarkson
-- 
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328










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

Reply via email to