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 { }

INIT{} won't do it.

> block at the beginning of the sub, would that do it?  Are there other
> ways?  Thanks.

yes. maintaining a variable to keep track of how many times the sub has been 
called like the others have suggested is one way. another way which does 
not require you to maintain a variable and allows you to cleanly separate 
the code for the first call and the second call with typeglob is also 
possible:

#!/usr/bin/perl

use strict;
use warnings;

no warnings 'redefine'; #-- because you know what you are doing, right?

sub first{

        print "first called\n";
        
        #--
        #-- your code here when it's first called
        #--

        *first = \&second;
}

sub second{

        print "second called\n";

        #--
        #-- your code here when it's called for the second time
        #--

        #--
        #-- notice you can cleanly separate the code from 'first()'
        #--
}

first;
first;

__END__

prints:

first called
second called

david
-- 
$_=q,015001450154015401570040016701570162015401440041,,*,=*|=*_,split+local$";
map{~$_&1&&{$,<<=1,[EMAIL PROTECTED]||3])=>~}}0..s~.~~g-1;*_=*#,

goto=>print+eval

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

Reply via email to