in reply to Re: "use strict" not too strict
in thread "use strict" not too strict

Having puttered, I share:
#!/usr/bin/perl -w use strict; use English; use vars qw($boo); $a = foo() if 0; sub bar {} END { no strict 'refs'; my $table = '::'; SYMBOL: for my $sym (sort {lc($a) cmp lc($b)} keys %{$table}) { next if $sym =~ /::$/ or $sym =~ /^[A-Z0-9]/; my $name = $table . $sym; next if $name =~ /^::\W/; foreach my $thing qw(ARRAY HASH IO) { if (defined *{$name}{$thing}) { next SYMBOL; } } print "Look at this!\n"; unless (defined *{$name}{CODE}) { warn "Possible undefined routine '$sym'"; } } }
The problem is that *{$name}{CODE} is as expected, undef for symbol foo, and not undef for symbol bar, but it is also undef for symbol boo. That is, there's no way to distinguish at the symbol table level between an undefined subprogram name, and a scalar, because both will have
defined(*foo{SCALAR}) && !defined(*foo{CODE})
Hmph.
--
Jeff Boes
Database Engineer
Nexcerpt, Inc.
vox 269.226.9550 ext 24
fax 269.349.9076
 http://www.nexcerpt.com
...Nexcerpt...Connecting People With Expertise

Replies are listed 'Best First'.
Re^3: "use strict" not too strict (go!)
by tye (Sage) on Jul 17, 2003 at 16:57 UTC

    I think this is a fantastic idea! I suspect that there is a way to distinguish these cases if someone dives a little deaper into the internals.

    I'd never thought of this idea, but upon hearing it, it is such a good idea that I'd be a bit surprised if someone hasn't already tried this and perhaps succeeded (or figured out exactly why you can't do it).

    Anyway, I'm very interested in seeing this tool become available. Please consider posting this as a new root node, especially if you don't get any useful responses in the next while.

    Thanks much. I'm looking forward to being able to do:

    perl -MDevel::ShowUndefSubs -c myScript

                    - tye
Re: Re: Re: "use strict" not too strict
by tilly (Archbishop) on Jul 17, 2003 at 17:40 UTC
    When I test it, it does not seem that END blocks run under -c. However a CHECK block will - and the line, return unless $^C; will properly make it only run if you are running under -c.

    Also it is a horrible hack, but you can test for scalars that have been declared with vars by evaling a piece of code which switches to that package and tries to access that variable. You can also test if the variable itself has a value. This doesn't, unfortunately, catch variables initialized with our. (Well I don't like our so I don't consider it that unfortunate...) However it catches vars, and it catches the common case of modules that initialize @ISA, etc while declaring them with our. (You also clear out a couple of global variables in main:: which are used by Perl internally.)

    If you turn this into a module then I would think carefully about how you want to control what will be checked for errors, allowing someone to readily decide that certain packages and functions are known to be OK, or only certain ones should be checked. After all the maximum utility will happen when you can check your code, see some errors, and then OK them as you figure out that they really are OK...

Re^3: "use strict" not too strict
by Aristotle (Chancellor) on Jul 17, 2003 at 19:22 UTC
    It is probably more fruitful to use the compiler backends (the B::* modules) than rely on the symbol table, or at least the symbol table alone.

    Makeshifts last the longest.

      I came across B::Lint, but it won't catch things like --
      use strict; my $a = foo() if 0;
      in that it still *runs* the code in question, and only flags undefined routines if they are actually *called*. Oh, well.
      --
      Jeff Boes
      Database Engineer
      Nexcerpt, Inc.
      vox 269.226.9550 ext 24
      fax 269.349.9076
       http://www.nexcerpt.com
      ...Nexcerpt...Connecting People With Expertise
        I was more talking about ::Xref and ::Terse/::Concise to inspect what symbols are actually used to look up subroutines through.

        Makeshifts last the longest.