Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Can I Force perl to Check Subroutine References at Compile Time?

by sierrathedog04 (Hermit)
on Jan 25, 2001 at 22:53 UTC ( #54322=perlquestion: print w/ replies, xml ) Need Help??
sierrathedog04 has asked for the wisdom of the Perl Monks concerning the following question:

Consider the test program
#!usr/bin perl -w use strict; if ("1" eq "2") { print hello(); } sub helloBROKEN { print "hello world\n"; }
This program will actually compile and run without error. If I remove the if ("1" eq "2") condition then I get an error saying that the subroutine does not exist.

I would like for perl to warn me whenever I refer to a subroutine which does not exist.

Does this behavior indicate that perl is only checking my subroutine references at runtime? Is there any way to force perl to check my subroutine references at compile time?

Comment on Can I Force perl to Check Subroutine References at Compile Time?
Download Code
Re: Can I Force perl to Check Subroutine References at Compile Time?
by Albannach (Prior) on Jan 25, 2001 at 22:58 UTC
    I think you're finding that Perl ignores the if statement as it can never be true. When you try another combination that at least has some hope of being true, Perl will report the error:
    my $foo = 1; if ($foo eq "1") { print hello(); } sub helloBROKEN { print "hello world\n"; }

    Update: The question said compile time - doh! - thanks eg, I need to pay more attention!

    Update 2: Thanks spaz! I admit I didn't deparse it so it's nice to see the confirmation.

    --
    I'd like to be able to assign to an luser

      This compiles fine (run it under perl -c.) If you want to get a compile-time error rather than a run-time error, you need to do what tye says below.

      cat >> test.pl #!/usr/bin/perl -w use strict; if( "1" eq "2" ) { print hello(); } sub helloBROKEN { print "Hello world\n"; } ---------------------------------- > perl -MO=Deparse test.pl test.pl syntax OK '???'; sub helloBROKEN { print "Hello world\n"; }
      You're exactly right about perl not even looking at that if statement.
(tye)Re: Can I Force perl to Check Subroutine References at Compile Time?
by tye (Cardinal) on Jan 25, 2001 at 23:05 UTC

    Sort your subroutine in reverse topological order (so subroutines are declared before you use them), then call all of your subroutines using barewords.

    Update: Might as well include a shameless plug where you can get more info on this and a few related things.

            - tye (but my friends call me "Tye")
Re: Can I Force perl to Check Subroutine References at Compile Time?
by boo_radley (Parson) on Jan 26, 2001 at 00:07 UTC
Re: Can I Force perl to Check Subroutine References at Compile Time?
by $code or die (Deacon) on Jan 26, 2001 at 02:51 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-11-28 07:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (193 votes), past polls