/scripts/tests/perl_swiss_army_knife.pl

https://code.google.com/p/camelbox/ · Perl · 86 lines · 48 code · 17 blank · 21 comment · 5 complexity · 9a1e65763c5e4ec3a60ff9a80513abbd MD5 · raw file

  1. #!/usr/bin/perl
  2. # $Id: perl_swiss_army_knife.pl 367 2008-07-20 17:48:23Z elspicyjack $
  3. # A script to print out a bunch of info about the current Perl environment
  4. # by Brian Manning (elspicyjack {at} gmail ⋅ com)
  5. # The original script most likely appears in the Perl Cookbook from O'Reilly.
  6. # if the script detects that it's running as under a CGI environment (the
  7. # REQUEST_METHOD environment variable is set), it will wrap the plaintext
  8. # output in the correct HTML tags so the browser will render it in the same
  9. # manner as if the script were running in a shell.
  10. my @found_modules; # a list of modules that were found in @INC paths
  11. my $i=1; # a counter
  12. # are we CGI?
  13. if ( exists $ENV{'REQUEST_METHOD'} ) {
  14. print "Content-type: text/html","\n\n";
  15. print "<html><body><pre>\n";
  16. } # if ( exists $ENV{'REQUEST_METHOD'} )
  17. print "##################################################################\n";
  18. print "# Perl Executable Name (\$^X) #\n";
  19. print "##################################################################\n";
  20. print qq(Executable name: $^X\n\n);
  21. print "##################################################################\n";
  22. print "# Perl Runtime Environment (\%ENV) #\n";
  23. print "##################################################################\n";
  24. # print the runtime environment
  25. foreach my $key ( sort(keys(%ENV)) ) {
  26. print(sprintf("%2d", $i) . qq( $key = ) . $ENV{$key} . qq(\n));
  27. $i++;
  28. } # while (($key, $val) = each %ENV)
  29. print "\n";
  30. $i=1; # reset counter
  31. # print the @INC array
  32. print "##################################################################\n";
  33. print "# Perl Module Include Paths (\@INC) #\n";
  34. print "##################################################################\n";
  35. printf qq(%2d %s\n), $i++, $_ for sort(@INC);
  36. print "\n";
  37. $i=0; # reset counter
  38. # print installed modules
  39. print "##################################################################\n";
  40. print "# Installed Perl Modules (\&modules in \@INC) #\n";
  41. print "##################################################################\n";
  42. # NOTES
  43. # 1. Prune man, pod, etc. directories
  44. # 2. Skip files with a suffix other than .pm
  45. # 3. Format the filename so that it looks more like a module
  46. # 4. Print it
  47. use File::Find;
  48. foreach $start ( @INC ) { find(\&modules, $start); }
  49. # reset counter
  50. my $i=1;
  51. foreach $module ( sort(@found_modules) ) {
  52. printf qq(%4d %s\n), $i++, $module;
  53. } # foreach $module ( sort(@found_modules) )
  54. # print the butt-end of the HTML if this is CGI
  55. if ( exists $ENV{'REQUEST_METHOD'} ) {
  56. print "</body></html>\n";
  57. } # if ( exists $ENV{'REQUEST_METHOD'} ) {
  58. exit 0;
  59. ### modules ###
  60. sub modules {
  61. if (-d && /^[a-z]/) { $File::Find::prune = 1; return; }
  62. return unless /\.pm$/;
  63. my $filename = "$File::Find::dir/$_";
  64. $filename =~ s!^$start/!!;
  65. $filename =~ s!\.pm$!!;
  66. $filename =~ s!/!::!g;
  67. push(@found_modules, $filename);
  68. $i++;
  69. } # sub modules