Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by sierrathedog04 (Hermit)
on Jan 25, 2001 at 22:53 UTC ( [id://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?

Replies are listed 'Best First'.
(tye)Re: Can I Force perl to Check Subroutine References at Compile Time?
by tye (Sage) 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 $code or die (Deacon) on Jan 26, 2001 at 02:51 UTC
Re: Can I Force perl to Check Subroutine References at Compile Time?
by Albannach (Monsignor) 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

      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.

      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.

Re: Can I Force perl to Check Subroutine References at Compile Time?
by boo_radley (Parson) on Jan 26, 2001 at 00:07 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-19 02:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found