testing for 'undefined subroutine'

  • Thread starter Torsten Mangner
  • Start date
T

Torsten Mangner

Hi group,

i need a capability (via a skript or something) to test my skripts (at
compile time), if all subroutines that are called in the programm are
really defined or 'use'd.

thanks,
Torsten
 
A

Anno Siegel

Torsten Mangner said:
Hi group,

i need a capability (via a skript or something) to test my skripts (at
compile time),

If you are using a script to check other scripts, the question of
"compile time" doesn't come up -- you won't be compiling the scripts
you are testing.
if all subroutines that are called in the programm are
really defined or 'use'd.

You don't "use" a subroutine, you "use" a module which may define some
subroutines.

In all its generality, your problem will be hard to solve. Since
subroutine calls can be modified at run time, there is no easy way
to know which subroutines will be called without running the program.
Since subroutine definitions can be added or modified at run time,
there is no way of telling which calls will succeeded without running
the program.

If you can settle for a fixed list of sub names, add this to your
script:

CHECK {
report_sub( $_) for qw( sub1 sub2 ...);

sub report_sub {
my $name = shift;
my $status = 'unknown';
$status = 'declared' if exists &$name;
$status = 'defined' if defined &$name;
print "$name is $status\n";
}
}

The CHECK block is run after compilation is complete (that means, all
"use" statements have been executed at this point). For the application
of "exists" and "defined" to subroutines, see "perldoc -f exists".

Anno
 
P

peter pilsl

Torsten said:
Hi group,

i need a capability (via a skript or something) to test my skripts (at
compile time), if all subroutines that are called in the programm are
really defined or 'use'd.

If you want to avoid the program fail because subroutines are not defined,
you could use an eval-statement around the part in question and query $@
for errors.
I use this method if calling methods defined in configuration-files.

peter
 
B

Bob Walton

Torsten Mangner wrote:

....
i need a capability (via a skript or something) to test my skripts (at
compile time), if all subroutines that are called in the programm are
really defined or 'use'd. ....

Torsten

You might try using the B::Xref module, perhaps as in:

perl -MO=Xref -c programname.pl

The listing from B::Xref should clue you in on the names of any subs
which aren't defined at compile time. It should also let you know about
any which are defined but not used anywhere, again as of compile time.

HTH.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,366
Latest member
IanCulpepp

Latest Threads

Top