[EMAIL PROTECTED] wrote:

> Hi
> I am writing a perl code which automatically forms a function name using
> certain criteria and then calls the same
>  
> I have a run time error reporting function not existing, if the function
> as such did not exist
> But I would like the execution to continue searching for other functions
>  
> So, is there any way I can find out if there is a function that is
> already existing to make a decision to call
>  
> I tired using the following code
> doesnt seem to work
>  
> my $func = \&TEST3;
> if (! $func) {
>   print "Function TEST3 does not exist\n";
> } else {
>   print "TEST3 Function Exists\n";
>   print &$func()." is return value\n";
> }
>  

There's probably a way, but can't think of one off-hand.

You could use a kludgy eval but it won't work if the function
returns undef - it would have to return a value :

my @funcs = (\&test1, \&test2, \&test3);
foreach (@funcs) {

        my $ret;
        eval "\$ret = &{\$_}";
        if (defined $ret) {
                print "Function - returns $ret\n";
        } else {
                print "Function - does not exist\n";
        }
}

# with the name rather than a ref

my @funcs = qw(test1 test2 test3);
foreach (@funcs) {

        my $ret;
        eval "\$ret = \&$_";
        if (defined $ret) {
                print "Function $_ - returns $ret\n";
        } else {
                print "Function $_ - does not exist\n";
        }
}
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to