assuring that all subroutines are defined

Bobby Kleemann rkleeman at neta.com
Thu May 18 13:55:31 CDT 2000


~sdpm~
On Thu, 18 May 2000, John R. Comeau wrote:

> ~sdpm~
> I asked a couple of people about this at the SDPM meeting last night.
> The question is how you can be certain that all subroutines called by
> your program actually exist.  That is, I'd like a way to find out if
> any subroutines are undefined as soon as the program starts to run
> instead of waiting until much later when the subroutine is actually
> called to find out.
> 
> I know this presents somewhat of a problem since new subroutines can
> be eval'ed into existence like the following:
> 
>     my $sub_definition = 'sub mysub {print "hello\n"}';
>     eval $sub_definition;
> 
> So it would be difficult for the Perl compiler to know beforehand
> whether a given subroutine will exist at the time it's called.
> 
> But disregarding this, is there some way to make Perl check that all
> subroutines are defined?
> 
> By the way, neither 'use strict' nor 'perl -w' guards against
> undefined subroutines.

Actually, I think use strict will protect you in a very limited sense (my
memory is a little hazy on this, so you'll have to experiment to see if
I'm completely correct).  Strict will say something when you try to do

	my_sub $param, @params;

and it hasn't seen or imported something like

sub my_sub {
	...
}

but if you put parens or & before your subroutine call it won't say
anything:

	&my_sub($param, @params);

But to answer your original question, I think you can do defined on a
subroutine name:

http://www.perl.com/pub/doc/manual/html/pod/perlfunc/defined.html

	You may also use defined() to check whether a subroutine exists,
	by saying defined &func without parentheses.
 _ _ _
 Bobby Kleemann <rkleeman at neta.com>
     http://www.neta.com/~rkleeman/

~sdpm~

The posting address is: san-diego-pm-list at hfb.pm.org

List requests should be sent to: majordomo at hfb.pm.org

If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at happyfunball.pm.org> with the following
command in the body of your email message:

    unsubscribe san-diego-pm-list

If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at happyfunball.pm.org> .
This is the general rule for most mailing lists when you need
to contact a human.




More information about the San-Diego-pm mailing list