| 1 | package Browser::LibModule; | ||
| 2 | my $RCSRevKey = '$Revision: 1.1.1.1 $'; | ||
| 3 | $RCSRevKey =~ /Revision: (.*?) /; | ||
| 4 | $VERSION=0.67; | ||
| 5 | use vars qw( @ISA @EXPORT @EXPORT_OK $VERSION ); | ||
| 6 | push @ISA, qw( Tk Exporter DB ); | ||
| 7 | |||
| 8 | @EXPORT_OK=qw($VERSION new retrieve readlib BaseName DESTROY); | ||
| 9 | |||
| 10 | require Exporter; | ||
| 11 | require Carp; | ||
| 12 | use File::Basename; | ||
| 13 | use Browser::LibModuleSymbol; | ||
| 14 | use Browser::LibSymbolRef; | ||
| 15 | |||
| 16 | my @modulepathnames; | ||
| 17 | my @libdirectories; | ||
| 18 | |||
| 19 | =head1 Browser::LibModule.pm - Perl Library Support for Tk::Browser.pm | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use Browser::LibModule; | ||
| 24 | use Browser::LibModuleSymbol; | ||
| 25 | use Browser::LibSymbolRef; | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | Browser::LibModule provides a Tk::Browser(3) with hierarchical object | ||
| 30 | references to Perl library modules, including package name, file name, | ||
| 31 | version, arrays of stash references, and superclasses, if any. | ||
| 32 | |||
| 33 | Browser::LibModule stores objects in a tree similar to the Perl class | ||
| 34 | hierarchy. The library module that the Browser::LibModule object | ||
| 35 | references need not be object oriented. Every module is a subclass of | ||
| 36 | Perl's default superclass UNIVERSAL. | ||
| 37 | |||
| 38 | Browser::LibModuleSymbol.pm provides lexical scanning and lookup, and | ||
| 39 | cross referencing subroutines. | ||
| 40 | |||
| 41 | Browser::LibSymbolRef provides a few methods for tied objects that correspond | ||
| 42 | to stash references. | ||
| 43 | |||
| 44 | =head2 Running Under Perl/Tk | ||
| 45 | |||
| 46 | Browser::LibModule call Tk::Event::DoOneEvent() to provide window | ||
| 47 | updates. The function usesTk() determines whether the module is | ||
| 48 | called from a program that uses Perl/Tk and returns true if called | ||
| 49 | from a program that has a Tk::MainWindow. Otherwise usesTk() returns | ||
| 50 | false. | ||
| 51 | |||
| 52 | =head1 REVISION | ||
| 53 | |||
| 54 | $Id: LibModule.pm,v 1.1.1.1 2015/04/18 18:43:42 rkiesling Exp $ | ||
| 55 | |||
| 56 | =head1 COPYRIGHT | ||
| 57 | |||
| 58 | Copyright © 2001-2004 Robert Kiesling, rkies@cpan.org. | ||
| 59 | |||
| 60 | Licensed using the same terms as Perl. Refer to the file, | ||
| 61 | "Artistic," for information. | ||
| 62 | |||
| 63 | =head1 SEE ALSO | ||
| 64 | |||
| 65 | Browser::LibModuleSymbol(3), Lib::SymbolRef(3), Tk::Browser(3), perlmod(1), | ||
| 66 | perlmodlib(1), perl(1). | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub new { | ||
| 71 | my $proto = shift; | ||
| 72 | my $class = ref( $proto ) || $proto; | ||
| 73 | my $self = { | ||
| 74 | children => [], | ||
| 75 | parents => '', | ||
| 76 | pathname => '', | ||
| 77 | basename => '', | ||
| 78 | packagename => '', | ||
| 79 | version => '', | ||
| 80 | superclasses => undef, | ||
| 81 | baseclass => '', | ||
| 82 | moduleinfo => undef, | ||
| 83 | symbols => [] | ||
| 84 | }; | ||
| 85 | bless( $self, $class); | ||
| 86 | return $self; | ||
| 87 | } | ||
| 88 | |||
| 89 | # Given a file base name, return the Module object. | ||
| 90 | sub retrieve { | ||
| 91 | my $parent = shift; | ||
| 92 | my ($n) = @_; | ||
| 93 | if ( $parent -> {basename} =~ /^$n/ ) { | ||
| 94 | return $parent; } | ||
| 95 | foreach ( @{$parent -> {children}} ) { | ||
| 96 | if ( $_ -> {basename} =~ /^$n/ ) { | ||
| 97 | return $_; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | foreach ( @{$parent -> {children}} ) { | ||
| 101 | if ( retrieve( $_, $n ) ) { | ||
| 102 | return $_; } | ||
| 103 | } | ||
| 104 | return undef; | ||
| 105 | } | ||
| 106 | |||
| 107 | # Given a file pathname, return the Module object. | ||
| 108 | sub pathname_retrieve { | ||
| 109 | my $parent = shift; | ||
| 110 | my ($n) = @_; | ||
| 111 | print "$n\n"; | ||
| 112 | if ( $parent -> {pathname} eq $n ) { | ||
| 113 | return $parent; } | ||
| 114 | foreach ( @{$parent -> {children}} ) { | ||
| 115 | if ( $_ -> {pathname} eq $n ) { | ||
| 116 | print $_ -> {pathname} . " $n \n"; | ||
| 117 | return $_; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | foreach ( @{$parent -> {children}} ) { | ||
| 121 | if ( retrieve( $_, $n ) ) { | ||
| 122 | return $_; } | ||
| 123 | } | ||
| 124 | return undef; | ||
| 125 | } | ||
| 126 | |||
| 127 | # Given a module package or sub-package name, return the module object. | ||
| 128 | # It's probably desirable to use this in preference to retrieve, | ||
| 129 | # with external calls, to avoid dealing with the library pathnames | ||
| 130 | # unless necessary. | ||
| 131 | sub retrieve_module { | ||
| 132 | my $parent = shift; | ||
| 133 | my ($n) = @_; | ||
| 134 | if ( $parent -> {packagename} eq $n ) { | ||
| 135 | return $parent; } | ||
| 136 | foreach ( @{$parent -> {children}} ) { | ||
| 137 | if ( $_ -> {packagename} eq $n ) { | ||
| 138 | return $_; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | foreach ( @{$parent -> {children}} ) { | ||
| 142 | if ( retrieve( $_, $n ) ) { | ||
| 143 | return $_; } | ||
| 144 | } | ||
| 145 | return undef; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub modulepathnames { | ||
| 149 | return @modulepathnames; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub libdirectories { | ||
| 153 | return @libdirectories; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub scanlibs { | ||
| 157 | my $b = shift; | ||
| 158 | my $m; | ||
| 159 | my ($path, $bname, $ext); | ||
| 160 | LOOP: foreach my $i ( @modulepathnames ) { | ||
| 161 | ($bname, $path, $ext) = fileparse($i, qw(\.pm$ \.pl$) ); | ||
| 162 | # Don't use RCS Archives or Emacs bacups | ||
| 163 | if( $bname =~ /(,v)|~/ ) { next LOOP; } | ||
| 164 | Tk::Event::DoOneEvent(255) if usesTk (); | ||
| 165 | if( $bname =~ /UNIVERSAL/ ) { | ||
| 166 | $b -> modinfo( $i ); | ||
| 167 | } else { | ||
| 168 | $m = new Browser::LibModule; | ||
| 169 | next LOOP if ! $m -> modinfo( $i ); | ||
| 170 | $m -> {parents} = $b; | ||
| 171 | push @{$b -> {children}}, ($m); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | sub modinfo { | ||
| 177 | my $self = shift; | ||
| 178 | my ($path) = @_; | ||
| 179 | my ($dirs, $bname, $ext); | ||
| 180 | my ($supers, $pkg, $ver, @text, @matches); | ||
| 181 | ($bname, $dirs, $ext) = fileparse($path, qw(\.pm \.pl)); | ||
| 182 | $self -> {pathname} = $path; | ||
| 183 | @text = $self -> readfile; | ||
| 184 | my $p = new Browser::LibModuleSymbol; | ||
| 185 | return undef if ! $p -> text_symbols( @text, $path ); | ||
| 186 | $self -> {moduleinfo} = $p ; | ||
| 187 | $self -> {packagename} = $p -> {packagename}; | ||
| 188 | $self -> {version} = $p -> {version}; | ||
| 189 | # We do a static match here because it's faster | ||
| 190 | # Todo: include base classes from "use base" statements. | ||
| 191 | @matches = grep /^\@ISA(.*?)\;/, @text; | ||
| 192 | $supers = $matches[0]; | ||
| 193 | $supers =~ s/(qw)|[=\(\)\;]//gms if $supers; | ||
| 194 | $self -> {basename} = $bname; | ||
| 195 | $self -> {superclasses} = $supers; | ||
| 196 | return 1; | ||
| 197 | } | ||
| 198 | |||
| 199 | # See the perlmod manpage | ||
| 200 | # Returns a hash of symbol => values. | ||
| 201 | # Handles as separate ref. | ||
| 202 | # Typeglob dereferencing deja Symdump.pm and dumpvar.pl, et al. | ||
| 203 | # Package namespace creation and module loading per base.pm. | ||
| 204 | sub exportedkeys { | ||
| 205 | my $m = shift; | ||
| 206 | my ($pkg) = @_; | ||
| 207 | my $obj; | ||
| 208 | my $packagekey; my $val; | ||
| 209 | my $rval; | ||
| 210 | my $nval; | ||
| 211 | my %keylist = (); | ||
| 212 | $m -> {symbols} = (); | ||
| 213 | my @vallist; | ||
| 214 | my $i = 0; | ||
| 215 | EACHKEY: foreach $packagekey ( keys %{*{"$pkg"}} ) { | ||
| 216 | next unless $packagekey; | ||
| 217 | if( defined ($val = ${*{"$pkg"}}{$packagekey} ) ) { | ||
| 218 | no warnings; # avoid uninitalized value warnings. | ||
| 219 | $rval = $val; $nval = $val; | ||
| 220 | $obj = tie $rval, 'Lib::SymbolRef', $packagekey; | ||
| 221 | push @{$m -> {symbols}}, ($obj); | ||
| 222 | foreach( @vallist) { if ( $_ eq $rval ) { next EACHKEY } } | ||
| 223 | # Replace the static $VERSION and @ISA values | ||
| 224 | # of the initial library scan with the symbol | ||
| 225 | # compile/run-time values. | ||
| 226 | local (*v) = $val; | ||
| 227 | # Look for the stash values in case they've changed | ||
| 228 | # from the source scan. | ||
| 229 | if( $packagekey =~ /VERSION/ ) { | ||
| 230 | $m -> {version} = ${*v{SCALAR}}; | ||
| 231 | } | ||
| 232 | if($packagekey =~ /ISA/ ) { | ||
| 233 | $m -> {superclasses} = "@{*v{ARRAY}}"; | ||
| 234 | } | ||
| 235 | use warnings; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | $keylist{$packagekey} = ${*{"$pkg"}}{$packagekey} if $packagekey; | ||
| 239 | # for dumping symbol refs to STDOUT. | ||
| 240 | # example of how to print listing of symbol refs. | ||
| 241 | # foreach my $i ( @{$m -> {symbols}} ) { | ||
| 242 | # foreach( @{$i -> {name}} ) { | ||
| 243 | # print $_; | ||
| 244 | # } | ||
| 245 | # print "\n--------\n"; | ||
| 246 | # } | ||
| 247 | return %keylist; | ||
| 248 | } | ||
| 249 | |||
| 250 | # | ||
| 251 | # Here for example only. This function (or the statements | ||
| 252 | # it contains), must be in the package that has the main:: stash | ||
| 253 | # space in order to list the packages symbols into the correct | ||
| 254 | # stash context. | ||
| 255 | # | ||
| 256 | # sub modImport { | ||
| 257 | # my ($pkg) = @_; | ||
| 258 | # eval "package $pkg"; | ||
| 259 | # eval "use $pkg"; | ||
| 260 | # eval "require $pkg"; | ||
| 261 | #} | ||
| 262 | |||
| 263 | sub readfile { | ||
| 264 | my $self = shift; | ||
| 265 | my $fn; | ||
| 266 | if (@_){ ($fn) = @_; } else { $fn = $self -> PathName; } | ||
| 267 | my @text; | ||
| 268 | open FILE, $fn or warn "Couldn't open file $fn: $!.\n"; | ||
| 269 | @text = <FILE>; | ||
| 270 | close FILE; | ||
| 271 | return @text; | ||
| 272 | } | ||
| 273 | |||
| 274 | # de-allocate module and all its children | ||
| 275 | sub DESTROY ($) { | ||
| 276 | my ($m) = @_; | ||
| 277 | @c = $m -> {children}; | ||
| 278 | $d = @c; | ||
| 279 | if ( $d == 0 ) { | ||
| 280 | $m = { | ||
| 281 | children => undef | ||
| 282 | }; | ||
| 283 | return; | ||
| 284 | } | ||
| 285 | foreach my $i ( @{$m -> {children}} ) { | ||
| 286 | Browser::LibModule -> DESTROY($i); | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | sub libdirs { | ||
| 291 | my $f; my $f2; | ||
| 292 | my $d; | ||
| 293 | foreach $d ( @INC ) { | ||
| 294 | push @libdirectories, ($d); | ||
| 295 | opendir DIR, $d; | ||
| 296 | @dirfiles = readdir DIR; | ||
| 297 | closedir DIR; | ||
| 298 | # look for subdirs of the directories in @INC. | ||
| 299 | foreach $f ( @dirfiles ) { | ||
| 300 | next if $f =~ m/^\.{1,2}$/ ; | ||
| 301 | $f2 = $d . '/' . $f; | ||
| 302 | if (opendir SUBDIR, $f2 ) { | ||
| 303 | push @libdirectories, ($f2); | ||
| 304 | &libsubdir( $f2 ); | ||
| 305 | closedir SUBDIR; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | } | ||
| 309 | } | ||
| 310 | |||
| 311 | sub libsubdir { | ||
| 312 | my ($parent) = @_; | ||
| 313 | opendir DIR, $parent; | ||
| 314 | my @dirfiles = readdir DIR; | ||
| 315 | closedir DIR; | ||
| 316 | foreach (@dirfiles) { | ||
| 317 | next if $_ =~ m/^\.{1,2}$/ ; | ||
| 318 | my $f2 = $parent . '/' . $_; | ||
| 319 | if (opendir SUBDIR, $f2 ) { | ||
| 320 | push @libdirectories, ($f2); | ||
| 321 | libsubdir( $f2 ); | ||
| 322 | closedir SUBDIR; | ||
| 323 | } | ||
| 324 | } | ||
| 325 | } | ||
| 326 | |||
| 327 | sub module_paths { | ||
| 328 | my $self = shift; | ||
| 329 | my ($f, $pathname, @allfiles); | ||
| 330 | Tk::Event::DoOneEvent(255) if usesTk (); | ||
| 331 | foreach ( @libdirectories ) { | ||
| 332 | opendir DIR, $_; | ||
| 333 | @allfiles = readdir DIR; | ||
| 334 | closedir DIR; | ||
| 335 | foreach $f ( @allfiles ) { | ||
| 336 | if ( $f =~ /\.p[lm]/ ) { | ||
| 337 | $pathname = $_ . '/' . $f; | ||
| 338 | push @modulepathnames, ($pathname); | ||
| 339 | } | ||
| 340 | } | ||
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | |||
| 345 | sub Children { | ||
| 346 | my $self = shift; | ||
| 347 | if (@_) { $self -> {children} = shift; } | ||
| 348 | return $self -> {children} | ||
| 349 | } | ||
| 350 | |||
| 351 | sub Parents { | ||
| 352 | my $self = shift; | ||
| 353 | if (@_) { $self -> {parents} = shift; } | ||
| 354 | return $self -> {parents} | ||
| 355 | } | ||
| 356 | |||
| 357 | sub PathName { | ||
| 358 | my $self = shift; | ||
| 359 | if (@_) { $self -> {pathname} = shift; } | ||
| 360 | return $self -> {pathname} | ||
| 361 | } | ||
| 362 | |||
| 363 | sub BaseName { | ||
| 364 | my $self = shift; | ||
| 365 | if (@_) { $self -> {basename} = shift; } | ||
| 366 | return $self -> {basename} | ||
| 367 | } | ||
| 368 | |||
| 369 | sub PackageName { | ||
| 370 | my $self = shift; | ||
| 371 | if (@_) { $self -> {packagename} = shift; } | ||
| 372 | return $self -> {packagename} | ||
| 373 | } | ||
| 374 | |||
| 375 | sub Symbols { | ||
| 376 | my $self = shift; | ||
| 377 | if (@_) { $self -> {symbols} = shift; } | ||
| 378 | return $self -> {symbols} | ||
| 379 | } | ||
| 380 | |||
| 381 | ### | ||
| 382 | ### Version, SuperClass -- Module.pm uses hashref directly. | ||
| 383 | ### | ||
| 384 | sub Version { | ||
| 385 | my $self = shift; | ||
| 386 | if (@_) { $self -> {version} = shift; } | ||
| 387 | return $self -> {version} | ||
| 388 | } | ||
| 389 | |||
| 390 | sub SuperClasses { | ||
| 391 | my $self = shift; | ||
| 392 | if (@_) { $self -> {superclasses} = shift; } | ||
| 393 | return $self -> {superclasses} | ||
| 394 | } | ||
| 395 | |||
| 396 | sub BaseClass { | ||
| 397 | my $self = shift; | ||
| 398 | if (@_) { $self -> {baseclass} = shift; } | ||
| 399 | return $self -> {baseclass} | ||
| 400 | } | ||
| 401 | |||
| 402 | sub ModuleInfo { | ||
| 403 | my $self = shift; | ||
| 404 | if (@_) { $self -> {moduleinfo} = shift; } | ||
| 405 | return $self -> {moduleinfo} | ||
| 406 | } | ||
| 407 | |||
| 408 | sub Import { | ||
| 409 | my ($pkg) = @_; | ||
| 410 | &Exporter::import( $pkg ); | ||
| 411 | } | ||
| 412 | |||
| 413 | sub usesTk { | ||
| 414 | return ( exists ${"main\:\:"}{"Tk\:\:"} ); | ||
| 415 | } | ||
| 416 | |||
| 417 | 1; | ||
| 418 |