We don't bite newbies here... much | |
PerlMonks |
Introspection, strict, warnings and "defined"by davies (Prior) |
on Dec 04, 2011 at 21:21 UTC ( [id://941764]=perlquestion: print w/replies, xml ) | Need Help?? |
davies has asked for the wisdom of the Perl Monks concerning the following question: I am trying to write some code that will act as a POD pre-processor. The idea is that anything looking like keyword will be checked against a list of subs in a module. If the suspected keyword is a sub name, the sub will be called. If not, the line will be treated as just some more POD text. To do this, I need to know what subs exist in the module that I am planning to write. Googling led me to this, which in turn gave me this. This second reference suggests that the same technique can be used for scalars, arrays and hashes. With significant help from Martin Berends, I got the code from the one liners given to the following:
This sort of works and is enough to do what I want, as I can get the list of subs that I need. But it doesn't work as I would expect and raises questions I don't know how to go about answering. 1. Is there any way to avoid the no strict 'refs' statement? I've never worked without strictures before and it leaves me feeling naked (not a pretty sight). 2. Is there any way to write a use statement to accept a variable without the eval technique Martin produced? It seems inelegant to me. 3. While the message is longer using diagnostics, nothing seems to be able to get rid of the run-time warning
Even commenting out strict, warnings and diagnostics fails to get rid of it. Removing the defined bit seems to work, but the code is very repetitive and my instincts are to refactor it to something DRYer, but that can't be done as elegantly if the hash version has to be materially different. I don't understand why that should be necessary. 4. This might well be part of the previous question - what is defined actually doing? My guess is that there is some DWIMmery going on. I have always used it to compare the value of a scalar to undef. Clearly it can't be doing this for subs, but what is happening to scalars? Is it returning those scalars that get assigned a value, or simply any scalar that exists? This latter view is the one implied by the second web page I link to above, written by someone with the knowledge to read MJD's code and understand it, i.e. someone who knows far more than I do. But in that case, it seems to be breaking down when looking for hashes. 5. Again, this may be related. If the code is run on Data::Dumper, almost all the scalars are the same as subs. The @all array I create contains only one reference to each name. I thought it was bad practice to have a scalar and a sub of the same name. Is my code faulty? Is my understanding faulty? Are there standards for using scalars with the same name as subs that anyone can point me to? As I said above, I have the code I need to get the list of subs, so the answers to this aren't terribly important. But my curiosity has been aroused. TIA & Regards, John Davies Update: fixed minor typo
Back to
Seekers of Perl Wisdom
|
|