| 1 | package NGS::Tools::BAMSurgeon::Helper; | ||
| 2 | |||
| 3 | # This package is primarily used to import other things. ToolSet makes this easier | ||
| 4 | use base 'ToolSet'; | ||
| 5 | |||
| 6 | use Data::Dump 'dump'; | ||
| 7 | use feature qw(say); | ||
| 8 | use Log::ProgramInfo; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | NGS::Tools::BAMSurgeon::Helper - A convenience module for importing general perl usefulness | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | use NGS::Tools::BAMSurgeon::Helper; | ||
| 20 | # We are now using strict, warnings, Carp, File::ShareDir, File::pushd, MooseX::Params::Validate | ||
| 21 | # You also have access to the following functions: | ||
| 22 | |||
| 23 | # Like print, but includes a newline automatically | ||
| 24 | say("Hello!"); | ||
| 25 | |||
| 26 | # both of these pretty print variable contents, even if they are arrays, hashes, nested, etc. | ||
| 27 | # (formatted printing via Data::Dump) | ||
| 28 | pretty_print(\@my_array); | ||
| 29 | # [2, 3, { 5 => 7 }] | ||
| 30 | dump(\@my_array); | ||
| 31 | # [2, 3, { 5 => 7 }] | ||
| 32 | |||
| 33 | # This lists all loaded modules, their versions, and the path they're loaded from | ||
| 34 | show_modules(); | ||
| 35 | # "NGS::Tools::BAMSurgeon::Helper" => [ | ||
| 36 | # "0.1.0", | ||
| 37 | # "/u/me/lib/NGS/Tools/BAMSurgeon/Helper.pm", | ||
| 38 | # ], | ||
| 39 | # "Carp" => [ | ||
| 40 | # 1.32, | ||
| 41 | # "/sw/perl/lib/site_perl/5.18.1/Carp.pm", | ||
| 42 | # ], | ||
| 43 | # ... | ||
| 44 | |||
| 45 | # This lists @INC, the search path for modules, in a nice format | ||
| 46 | show_inc(); | ||
| 47 | # [ | ||
| 48 | # "/u/me/repos/modulename/blib/lib", | ||
| 49 | # ".", | ||
| 50 | # ... | ||
| 51 | # ] | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | ToolSet->use_pragma('strict'); | ||
| 56 | ToolSet->use_pragma('warnings'); | ||
| 57 | |||
| 58 | # Make the say function work | ||
| 59 | ToolSet->use_pragma( qw(feature say) ); | ||
| 60 | |||
| 61 | # define exports from other modules | ||
| 62 | ToolSet->export( | ||
| 63 | Carp => undef, # get the defaults | ||
| 64 | Data::Dump => 'dump', | ||
| 65 | File::pushd => undef, | ||
| 66 | File::ShareDir => undef, | ||
| 67 | MooseX::Params::Validate => undef | ||
| 68 | ); | ||
| 69 | |||
| 70 | our @EXPORT = qw(pretty_print show_modules show_inc); | ||
| 71 | |||
| 72 | # Nice data structures | ||
| 73 | sub pretty_print { | ||
| 74 | dump(@_); | ||
| 75 | } | ||
| 76 | |||
| 77 | # Print version and loading path information for modules | ||
| 78 | sub show_modules { | ||
| 79 | my $module_infos = {}; | ||
| 80 | |||
| 81 | # %INC looks like this: | ||
| 82 | # { | ||
| 83 | # ... | ||
| 84 | # "Data/Dump.pm" | ||
| 85 | # => "/whatever/perl/lib/site_perl/5.18.1/Data/Dump.pm", | ||
| 86 | # ... | ||
| 87 | # } | ||
| 88 | # So let's convert it to this: | ||
| 89 | # { | ||
| 90 | # ... | ||
| 91 | # "Data/Dump.pm" | ||
| 92 | # => [ "1.4.2", | ||
| 93 | # "/whatever/perl/lib/site_perl/5.18.1/Data/Dump.pm", | ||
| 94 | # ], | ||
| 95 | # ... | ||
| 96 | # } | ||
| 97 | foreach my $module_inc_name ( keys(%INC) ) { | ||
| 98 | my $real_name = $module_inc_name; | ||
| 99 | $real_name =~ s|/|::|g; | ||
| 100 | $real_name =~ s|\.pm$||; | ||
| 101 | |||
| 102 | my $version = eval { $real_name->VERSION } | ||
| 103 | // eval { ${"${real_name}::VERSION"} } | ||
| 104 | // 'unknown'; | ||
| 105 | # stringify, in case it is a weird format | ||
| 106 | # - I don't think the 'invalid' alternative can be hit, but safer to have it in | ||
| 107 | $version = eval { $version . '' } // 'invalid'; | ||
| 108 | |||
| 109 | $module_infos->{$real_name} = [ $version, $INC{$module_inc_name} ]; | ||
| 110 | } | ||
| 111 | |||
| 112 | return $module_infos if defined wantarray(); | ||
| 113 | |||
| 114 | say( "Modules currently found are:\n", dump($module_infos) ); | ||
| 115 | } | ||
| 116 | |||
| 117 | # Print @INC nicely | ||
| 118 | sub show_inc { | ||
| 119 | my $result = dump( \@INC ); | ||
| 120 | return $result if defined wantarray(); | ||
| 121 | say( "Modules are currently being looked for in these locations:\n", $result ); | ||
| 122 | } | ||
| 123 | |||
| 124 | 1; |