/usr/share/perl5/SHARYANTO/Package/Util.pm is in libsharyanto-utils-perl 0.53-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | package SHARYANTO::Package::Util;
use 5.010;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(
package_exists
list_package_contents
list_subpackages
);
our $VERSION = '0.53'; # VERSION
sub package_exists {
no strict 'refs';
my $pkg = shift;
return unless $pkg =~ /\A\w+(::\w+)*\z/;
if ($pkg =~ s/::(\w+)\z//) {
return !!${$pkg . "::"}{$1 . "::"};
} else {
return !!$::{$pkg . "::"};
}
}
# XXX incomplete/improper
sub list_package_contents {
no strict 'refs';
my $pkg = shift;
return () unless !length($pkg) || package_exists($pkg);
my $symtbl = \%{$pkg . "::"};
my %res;
while (my ($k, $v) = each %$symtbl) {
next if $k =~ /::$/; # subpackage
my $n;
if ("$v" !~ /^\*/) {
# constant
$res{$k} = $v;
next;
}
if (defined *$v{CODE}) {
$res{$k} = *$v{CODE}; # subroutine
$n++;
}
if (defined *$v{HASH}) {
$res{"\%$k"} = \%{*$v}; # hash
$n++;
}
if (defined *$v{ARRAY}) {
$res{"\@$k"} = \@{*$v}; # array
$n++;
}
if (defined(*$v{SCALAR}) # XXX always defined?
&& defined(${*$v})) { # currently we filter undef values
$res{"\$$k"} = \${*$v}; # scalar
$n++;
}
if (!$n) {
$res{"\*$k"} = $v; # glob
}
}
%res;
}
sub list_subpackages {
no strict 'refs';
my ($pkg, $recursive, $cur_res) = @_;
return () unless !length($pkg) || package_exists($pkg);
my $symtbl = \%{$pkg . "::"};
my $res = $cur_res // [];
while (my ($k, $v) = each %$symtbl) {
next unless $k =~ s/::$//;
my $name = (length($pkg) ? "$pkg\::" : "" ) . $k;
push @$res, $name;
list_subpackages($name, 1, $res) if $recursive;
}
@$res;
}
1;
# ABSTRACT: Package-related utilities
__END__
=pod
=encoding utf-8
=head1 NAME
SHARYANTO::Package::Util - Package-related utilities
=head1 VERSION
version 0.53
=head1 SYNOPSIS
use SHARYANTO::Package::Util qw(
package_exists
list_package_contents
list_subpackages
);
print "Package Foo::Bar exists" if package_exists("Foo::Bar");
my %content = list_package_contents("Foo::Bar");
my @subpkg = list_subpackages("Foo::Bar");
my @allsubpkg = list_subpackages("Foo::Bar", 1); # recursive
=head1 DESCRIPTION
=head2 package_exists($name) => BOOL
Return true if package "exists". By "exists", it means that the package has been
defined by C<package> statement or some entries have been created in the symbol
table (e.g. C<$Foo::var = 1;> will make the C<Foo> package "exist").
This function can be used e.g. for checking before aliasing one package to
another. Or to casually check whether a module has been loaded.
=head2 list_package_contents($name) => %res
Return a hash containing package contents. For example:
(
sub1 => \&Foo::Bar::sub1,
sub2 => \&Foo::Bar::sub2,
'%h1' => \%Foo::Bar::h1,
'@a1' => \@Foo::Bar::a1,
...
)
This module won't list subpackages. Use list_subpackages() for that.
=head2 list_subpackages($name[, $recursive]) => @res
List subpackages, e.g.:
(
"Foo::Bar::Baz",
"Foo::Bar::Qux",
...
)
If $recursive is true, will also list subpackages of subpackages, and so on.
=head1 SEE ALSO
L<perlmod>
=head1 AUTHOR
Steven Haryanto <stevenharyanto@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2013 by Steven Haryanto.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 FUNCTIONS
None are exported by default, but they are exportable.
=cut
|