How to catch refs to nonexistent sub names at compilation time ?

Y

Yakov

I use 'usr strict; use warnings'. It catches uses of nonexistent
variables,
at compile time, very good. But it does not catch uses of nonexistent
subroutines
at compile time. Until error at runtime. Too late.

Is there any directive to catch uses of nonexistent subroutines at
compile-time ?
Example:

perl -e 'use strict; use warnings; if(int(rand(2)))
{ NoSuchSub(); }'

Depending on your luck, this program either works, or dies.
But I'd like to catch ref to apparently nonexistent subroutine at
compile-time.

Thanks
Y.L.
 
P

Peter Scott

But I'd like to catch ref to apparently nonexistent subroutine at
compile-time.

The reason this hasn't been done is that in general, it is impossible.
Try doing it for this code:

chomp(my $sub = <STDIN>);
$sub->(42);
sub foo { print shift }

What subroutine does $sub refer to? So this is only possible by ignoring
the possibility of dynamically constructed subroutine references. And
doing it for method calls is hair raising.

However, there is a discussion about it over in comp.lang.perl.modules at
the moment; you might drop in on that and see whether something gets
released.
 
B

Ben Morrow

Quoth Peter Scott said:
The reason this hasn't been done is that in general, it is impossible.
Try doing it for this code:

chomp(my $sub = <STDIN>);
$sub->(42);
sub foo { print shift }

Can't use string ("foo") as a subroutine ref while "strict refs" is in
use at ...
What subroutine does $sub refer to? So this is only possible by ignoring
the possibility of dynamically constructed subroutine references. And
doing it for method calls is hair raising.

Method calls are a whole nother kettle of fish.

Ben
 

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
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top