UPDATE - Okay, Devel::GetSymbols it will be. I also added subs(). Updated code below.
Hi fellow monks,
Once in a while, one of us asks how all subs can be exported. Some want a list of subs in a certain package for debugging (I'd use it on a badly documented OO XS module).
We can already do that! We don't need a module for that!
However, the code that's used for that, depends on a lot of magic, and I don't think it really looks clean. I'd rather type it once, and then just use a nice interface to it.
Having this in a module would also save time when someone asks how all subs can easily be exported. We could just answer
use Symbol::List;
@EXPORT_OK = Symbol::List::symbols('CODE');
and go on with our own hacking :)
I present you the module here, before I upload it to CPAN. If you feel it needs a change, or think CPAN should not be poluted with such crap, please do respond. If you like the idea and would love to have it on CPAN, please tell why. It's also possible this module already exists under a different name - please let me know.
(The code is after the readmore tag)
Package Devel::GetSymbols;
use strict;
use vars qw($VERSION @EXPORT_OK);
use base 'Exporter';
use Carp;
@EXPORT_OK = qw(symbols subs);
$VERSION = '0.01';
no strict 'refs';
sub symbols {
my ($type, $package) = @_;
$package = (caller)[0] unless defined $package;
croak 'Usage: symbols(type[, package])' unless defined $type;
grep defined *{"${package}::$_"}{$type}, keys %{"${package}::"}
}
sub subs () { symbols( CODE => (caller)[0] ) }
1;
__END__
=head1 NAME
Devel::GetSymbols - get a list of symbols that match a certain type
=head1 SYNOPSIS
use Devel::GetSymbols qw(symbols subs);
my @subroutines = symbols('CODE');
my @subroutines = subs(); # equal
# @subroutines includes 'symbols', because it was imported
# into this package.
use Data::Dumper;
print "$_\n" for symbols('SCALAR', 'Data::Dumper');
use base 'Exporter';
@EXPORT_OK = symbols('CODE'); # Yuch.
@EXPORT_OK = subs(); # Ditto.
@EXPORT = grep /^[A-Z_]+$/, symbols('CODE'); # Export constants
=head1 DESCRIPTION
This module just does a grep on some keys, but can save a lot of devel
+opment
time.
=over 10
=item symbols
Takes type and optional package name as arguments and returns a list o
+f symbols
that are of that type and in that package. Defaults to the calling pac
+kage.
Type can be one of qw(ARRAY CODE FORMAT GLOB HASH IO SCALAR). I guess
+C<CODE>
will be used most :)
=item subs
Shorthand for C<symbols('CODE')>. Does not take arguments (use C<symbo
+ls>
if you want to get it from another package).
=back
=head1 KNOWN BUGS
None yet
=head1 AUTHOR
Juerd <juerd@juerd.nl>
=cut
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.