allsubs.pm

Alex Davies suggested in comp.lang.perl.misc that it would be nice if there was a way to automatically predeclare all your subroutines, and presented sample code for doing it. Then he made the mistake of asking if it was a bad idea.

I thought it sounded like a totally stupid idea, but that didn't stop me from working on it. Sometimes I work on a stupid idea and it turns out to be really clever. So here it is.

View or download source code

To use it, just add use allsubs; at the top of your file.

Notes and Caveats

  1. allsubs reads the program source code, looking for subroutines. That means that if you have sufficiently bizarre source code layout, you can confuse it. For example:

    	sub
    	fred
    	{ $_[0]{F} = qq{
    	sub actually_this_is_really_a_string { "that looks like a sub" }
    	             };
            }
    

    Here it's going to think you have a subroutine called actually_this_is_really_a_string. But you don't; actually it's really a string.

    So don't do that. Uri Guttman says that makes it `broken', but so what? By that standard lots of useful stuff is `broken'. ptags is `broken' in exactly the same way, but that doesn't stop me from using it joyfully every day.

  2. When I wrote it, I tried to make it so that it would work if you used it in a module. But I've never tested it. Let me know if it works.

  3. It caches the function declarations in a separate file; next time you run it, it tries to read this file back in at compile time instead of grovelling over the source code. If the file is out-of-date, it rebuilds it. The declarations for the file foo.pl are stored in (where else?) foo.h.

    If you're sure that .h file is good, and should never be rebuilt, add this line:

    	$H_FROZEN = 1;
    

    anywhere in the .h file. If it has that, it won't be rebuilt even if it's out of date.

  4. Yes, it does handle prototypes properly.

  5. I tried to get it to work with __DATA__, to eleminate the dependency on $0, but that didn't work, because __DATA__ isn't opened until it's too late.

  6. I truly can't decide whether this is a good idea or not. It seems awfully stupid for some reason, and yet I can't actually come up with a coherent argument about why it's a bad idea. So if you want to weigh in one way or the other, please send me mail.


Return to: Universe of Discourse main page | What's new page | Perl Paraphernalia

mjd-perl-allsubs@plover.com