[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] ignoring undefined subroutines



Terry wrote:
> I want to ignore undefined subroutines.  Right now, my collection of
> text ( not worthy of being called a perl script ) calls subroutines
> but I want it to NOT die when it comes across a subroutine that is
> not defined.  How can I do this?   For example:
>
> &{"header_$who"};
>
> if $who is not defined or contains a goofy value, my script dies,
> I do not want this.

Jay's AUTOLOAD seems a good solution. However, because variety is the
spice of life, here are two different approaches:

sub header_hugh { print "in hugh\n" }
for my $who ( 'dummy', 'hugh' ) {
    eval { &{"header_$who"} };
    print "$who died\n" if $@;
}

which simply traps die with eval. Notice that this will also catch
_any_ die inside the called routine (I suppose you could tell the
difference by examining the contents of $@) which may be a bug
or a feature depending on your needs.

Alternatively, you might peek at the symbol table before attempting
to call the function (if you knew which package it was defined in).
For example:

use strict;
sub header_hugh { print "in hugh\n" }
for my $who ( 'dummy', 'hugh' ) {
    if (exists($main::{"header_$who"})) {
        &{$main::{"header_$who"}};
    } else {
        print "no such function $who\n";
    }
}

Hugh.



		
__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail