testing for 'undefined subroutine'

Discussion in 'Perl Misc' started by Torsten Mangner, Nov 4, 2003.

  1. 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

    --
    Torsten Mangner
    http://www.alphaone.de

    I'm not lost, I'm directionally challenged.
     
    Torsten Mangner, Nov 4, 2003
    #1
    1. Advertising

  2. Torsten Mangner

    Anno Siegel Guest

    Torsten Mangner <> wrote in comp.lang.perl.misc:
    > 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
     
    Anno Siegel, Nov 4, 2003
    #2
    1. Advertising

  3. Torsten Mangner

    peter pilsl Guest

    Torsten Mangner wrote:

    > 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


    --
    peter pilsl

    http://www.goldfisch.at
     
    peter pilsl, Nov 4, 2003
    #3
  4. Torsten Mangner

    Bob Walton Guest

    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.
    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Nov 5, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    0
    Views:
    508
  2. Replies:
    4
    Views:
    200
    Bob Walton
    Aug 12, 2003
  3. Moulin Kluge
    Replies:
    2
    Views:
    391
    pkent
    Jan 27, 2004
  4. Dan Burke
    Replies:
    6
    Views:
    272
    Dan Burke
    Jul 5, 2004
  5. king
    Replies:
    5
    Views:
    210
Loading...

Share This Page