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:

use strict; use warnings; use diagnostics; no strict 'refs'; my $pkg = shift; eval("use $pkg"); my @all = keys %{$pkg . "::"}; print "Subs:\n"; my %subs; for (@all) { if (defined &{$pkg . "::$_"}) { $subs{$_} = ''; } } print join(", ", sort keys %subs), "\n"; print "\nVars:\n"; my %vars; for (@all) { if (defined ${$pkg . "::$_"}) { $vars{$_} = ''; } } print join(", ", sort keys %vars), "\n"; print "\nArrs:\n"; my %arrs; for (@all) { if (defined @{$pkg . "::$_"}) { $arrs{$_} = ''; } } print join(", ", sort keys %arrs), "\n"; print "\nHash:\n"; my %hash; for (@all) { if (defined %{$pkg . "::$_"}) { $hash{$_} = ''; } } print join(", ", sort keys %hash), "\n";

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

defined(%hash) is deprecated at Z:\Data\Perl\PkgName.pl line 39 (#1) (D deprecated) defined() is not usually useful on hashes because i +t checks for an undefined scalar value. If you want to see if the h +ash is empty, just use if (%hash) { # not empty } for example. (Maybe you should just omit the defined()?)

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


In reply to Introspection, strict, warnings and "defined" by davies

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.