Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How to check program links correctly?

by johnnywang (Priest)
on Aug 20, 2004 at 00:00 UTC ( [id://384498]=perlquestion: print w/replies, xml ) Need Help??

johnnywang has asked for the wisdom of the Perl Monks concerning the following question:

Please help me with some concepts.

"perl -c foo.pl" only checks the syntex error, so even the following passes:

use strict; foo();
The same is true for:
use strict; use FOO; #assuming it exists my $foo = Foo->new(); $foo->some_non_existing_function();
Is there anyway to check this type of linking error? I guess since one can use a variable as a function name, one can't really check all except running the program. But I'd like to do a "build" on all my perl scripts/modules, and catch this type of error, is that possible? (this is related to: Perl and code reviews

Replies are listed 'Best First'.
•Re: How to check program links correctly?
by merlyn (Sage) on Aug 20, 2004 at 00:04 UTC
Re: How to check program links correctly?
by tachyon (Chancellor) on Aug 20, 2004 at 00:48 UTC

    As merlyn points out you can not check for this in the same way you can with say C. The reason is simple - although some_non_existing_function() may not exist at compile time, because you can generate functions dynamically in perl *it may well exist by the time it is called*. Consider for example AUTOLOAD which is typically used to dynamically generate non existent accessor functions on demand.

    I have always vaguely felt there should be a use strict 'functions' pragma.

    cheers

    tachyon

Re: How to check program links correctly?
by tachyon (Chancellor) on Aug 20, 2004 at 04:01 UTC

    You may find an AUTOLOAD like this of benefit:

    package Foo; Bar::baz( 1, 2, [3,4,5] ); package Bar; sub baz{ &Baz::qux } package Baz; sub AUTOLOAD { require Carp; require Data::Dumper; warn "Non-existant function $AUTOLOAD called with args:\n", Data::Dumper::Dumper(@_), "\nStacktrace:\n"; Carp::confess(); } __DATA__ Non-existant function Baz::qux called with args: $VAR1 = 1; $VAR2 = 2; $VAR3 = [ 3, 4, 5 ]; Stacktrace: at script line 16 Baz::AUTOLOAD called at script line 7 Bar::baz(1, 2, 'ARRAY(0x1abf188)') called at script line 3

    cheers

    tachyon

Re: How to check program links correctly?
by TilRMan (Friar) on Aug 20, 2004 at 09:50 UTC

    There's a module called B::Lint that can check for undefined subroutines (but not methods). I've had mixed success with it.

    <rant>

    This is my single biggest pet peeve with Perl. The whole 'But it might be defined when you get around to calling it' excuse is a cop-out. In most production code, if your subroutine isn't defined (or at least declared) by the time CHECK rolls around, it's probably not going to be. The inconvenience of having to declare your subroutines at compile time is far less than that of your script bailing at run time.

    I like tachyon's idea of use strict 'functions', though I'd like it even better if it was part of vanilla use strict.

    </rant>

    Maybe this would be a good reason for me to finally learn some Perl internals.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://384498]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found