Here are the guts of a module that I'm working on called Globwalker.pm. It's supposed to give you a list of objects of a given type in a given typeglob (e.g. for example all subs in main::). It seem to work OK for most glob types, but I'm getting strange results for scalars. I assume this is because the scalar knob is defined as soon as the typeglob comes into existance.

Does anyone have any idea how I can work out if the scalar has really been used?

package Globwalker; use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); require Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw(get_things get_subs get_scalars get_arrays get_hashes get_filehandles); $VERSION = '0.01'; sub get_things { my $thing = shift; my $pkg = shift || caller; my @things; no strict 'refs'; # WARNING: Deep magic here! while (my ($sym_name, $sym_glob) = each %{"${pkg}::"}) { push @things, $sym_name if defined *$sym_glob{$thing}; } @things; } sub get_subs { get_things('CODE', shift || caller) }; sub get_scalars { get_things('SCALAR', shift || caller) }; sub get_arrays { get_things('ARRAY', shift || caller) }; sub get_hashes { get_things('HASH', shift || caller) }; sub get_filehandles { get_things('IO', shift || caller) }; 1;
--
<http://www.dave.org.uk>

"Perl makes the fun jobs fun
and the boring jobs bearable" - me


In reply to Finding out if a scalar exists in a typeglob by davorg

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.