#!/usr/bin/perl -w # NAME: perl-list.pl # AIM: SPECIALISED - Just to make a perl-list.txt and perl-list.htm file # 21/07/2014 - see also genreadme.pl, whihc does a similar job! # 23/12/2011 geoff mclane http://geoffair.net/mperl use strict; use warnings; use File::Basename; # split path ($name,$dir,$ext) = fileparse($file [, qr/\.[^.]*/] ) use File::stat; use Cwd; my $perl_dir = 'C:\GTools\perl'; unshift(@INC, $perl_dir); require 'lib_utils.pl' or die "Unable to load 'lib_utils.pl' Check paths in \@INC...\n"; # log file stuff our ($LF); my $pgmname = $0; if ($pgmname =~ /(\\|\/)/) { my @tmpsp = split(/(\\|\/)/,$pgmname); $pgmname = $tmpsp[-1]; } my $outfile = $perl_dir."\\temp.$pgmname.txt"; open_log($outfile); # user variables my $VERS = "0.0.1 2011-12-22"; my $load_log = 1; my $in_file = ''; my $verbosity = 0; my $debug_on = 0; my $def_file = 'def_file'; my $out_file = $perl_dir.'\perl-list.txt'; my $show_type_text = 0; my $show_path = 0; my $out_html = $perl_dir.'\perl-list.htm'; my $tab_space = ' '; # note tabs to 3 spaces - change if desired my $add_top_jump = 1; my $add_alpha_list = 1; ### program variables my @g_file_list = (); my @warnings = (); my $cwd = cwd(); my $os = $^O; my $out_text = ''; my $html_text = ''; my %name_dupes = (); my $date_key = '0001_date'; my $size_key = '0002_size'; my $aim_key = '0003_AIM'; my $use_key = '0004_use'; my $sub_key = '0005_sub'; sub VERB1() { return $verbosity >= 1; } sub VERB2() { return $verbosity >= 2; } sub VERB5() { return $verbosity >= 5; } sub VERB9() { return $verbosity >= 9; } sub show_warnings($) { my ($val) = @_; if (@warnings) { prt( "\nGot ".scalar @warnings." WARNINGS...\n" ); foreach my $itm (@warnings) { prt("$itm\n"); } prt("\n"); } else { prt( "\nNo warnings issued.\n\n" ) if (VERB9()); } } sub pgm_exit($$) { my ($val,$msg) = @_; if (length($msg)) { $msg .= "\n" if (!($msg =~ /\n$/)); prt($msg); } show_warnings($val); close_log($outfile,$load_log); exit($val); } sub prtw($) { my ($tx) = shift; $tx =~ s/\n$//; prt("$tx\n"); push(@warnings,$tx); } my %g_hash = (); my $TYP_HASH = 1; my $TYP_ARRAY = 2; my $TYP_CODE = 3; my $TYP_GLOB = 4; my $TYP_OTHER = 5; my $TYP_UNDEF = 6; my %type_text = ( $TYP_HASH => 'HASH', $TYP_ARRAY => 'ARRAY', $TYP_CODE => 'CODE', $TYP_GLOB => 'GLOB', $TYP_OTHER => 'OTHER', $TYP_UNDEF => 'TEXT' ); sub get_type_number($) { my ($k) = shift; my $type = ref($k); if ($type) { if ($type eq 'HASH' ) { return $TYP_HASH; } elsif ($type eq 'ARRAY') { return $TYP_ARRAY; } elsif ($type eq 'CODE') { return $TYP_CODE; } elsif ($type eq 'GLOB') { return $TYP_GLOB; } else { return $TYP_OTHER; } } return $TYP_UNDEF; } sub get_type_text($) { my $num = shift; if (defined $type_text{$num}) { return $type_text{$num}; } return 'Undefined $num'; } sub is_text_type($) { my ($k) = shift; my $n = get_type_number($k); return 1 if ($n == $TYP_UNDEF); return 0; } ###################################################### # Converting SPACES to ' ' # Of course this could be done just using perl's # powerful search and replace, but this handles # any number of spaces, only converting the number # minus 1 to ... not sure how to have # this level of control with regex replacement ###################################################### sub conv_spaces { my $t = shift; my ($c, $i, $nt, $ln, $sc, $sp); $nt = ''; # accumulate new line here $ln = length($t); for ($i = 0; $i < $ln; $i++) { $c = substr($t,$i,1); if ($c eq ' ') { $i++; # bump to next $sc = 0; $sp = ''; for ( ; $i < $ln; $i++) { $c = substr($t,$i,1); if ($c ne ' ') { last; # exit } $sc++; $sp .= $c; } if ($sc) { $sp =~ s/ / /g; $nt .= $sp; } $i--; # back up one $c = ' '; # add back the 1 space } $nt .= $c; } prt( "conv_space: from [$t] to [$nt] ...\n" ) if $debug_on; return $nt; } ########################################################################### # VERY IMPORTANT SERVICE # This converts the 'text' into HTML text, but only does a partial job! # 1. Convert '&' to '&' to avoid interpreting as replacement # 2. Convert '<' to '<' to avoid interpreting as HTML # 3. Convert '>' to '>' to avoid interpreting as HTML # 4. Convert '"' to '"' # 5. Convert '\t' to SPACES # 6. Finally, if there are double or more SPACES, convert to ' ' ########################################################################### sub html_line { my $t = shift; my $ot = $t; $t =~ s/&/&/g; # all '&' become '&' $t =~ s/</g; # make sure all '<' is/are swapped out $t =~ s/>/>/g; # make sure all '>' is/are swapped out $t =~ s/\"/"/g; # and all quotes become " $t =~ s/\t/$tab_space/g; # tabs to spaces if ($t =~ /\s\s/) { # if any two consecutive white space return conv_spaces($t); } prt( "html_line: from [$ot] to [$t] ...\n" ) if $debug_on; return $t; } sub show_hash_type($$$$); sub show_array_type($$$$); sub show_hash_type($$$$) { my ($data,$dep,$path,$inkey) = @_; my $cnt = 0; my $indent = ' ' x $dep; my ($key,$val,$num,$text,$icnt,$npath,$ccnt,$hcnt); $num = get_type_number($data); $text = ($show_type_text ? get_type_text($num) : ""); $hcnt = scalar keys %{$data}; $npath = $show_path ? $path : ""; prt($indent."Count $hcnt $text $npath\n" ) if (VERB1()); foreach $key (sort keys %{$data}) { $val = ${$data}{$key}; $num = get_type_number($val); $text = ($show_type_text ? get_type_text($num) : ""); $cnt++; $ccnt = sprintf("%2d",$cnt); ###$npath = $path.'->'.'{"'.$key.'"}'; $npath = $show_path ? $path.'{"'.$key.'"}' : ""; $key =~ s/^\d+_//; if ($num == $TYP_UNDEF) { prt( $indent."$ccnt: $key = ".$val." $text $npath\n" ); if ($key eq 'date') { $out_text .= " $val"; $html_text .= " ".html_line(trim_all($val)); } elsif ($key eq 'size') { $out_text .= " $val\n"; $html_text .= " ".html_line(trim_all($val))."\n"; } } elsif ($num == $TYP_HASH) { $icnt = scalar keys(%{$val}); prt($indent."$ccnt: $key $text $icnt $npath\n"); # if (VERB2()); show_hash_type($val,($dep+1),$npath,$key); } elsif ($num == $TYP_ARRAY) { $icnt = scalar @{$val}; prt($indent."$ccnt: $key $text $icnt $npath\n"); # if (VERB2()); if ($key eq 'use') { $out_text .= "use "; $html_text .= "\n
Alpha-List: "; my ($file,$jump,$key,$val,$fnd); foreach $file (@list) { foreach $key (keys %name_dupes) { $val = $name_dupes{$key}; if ($val eq $file) { $jump = $key; $fnd = 1; last; } } if ($fnd) { $txt .= "\n$file"; } else { prtw("WARNING: Failed to find file [$file] in name_dupes hash!\n"); } } $txt .= "
\n"; return $txt; } sub process_rh($) { my ($rh) = @_; my ($key,$val,$num,$txt,$cnt,$npath,$icnt,$ccnt,$htm_key); $cnt = 0; my $path = '${$rh}'; $num = get_type_number($rh); $txt = get_type_text($num); $icnt = scalar keys(%{$rh}); prt("Count $icnt keys in $txt $path\n" ); # if (VERB1()); foreach $key (sort cmpNoCase keys %{$rh}) { $val = ${$rh}{$key}; $cnt++; $ccnt = sprintf("%3d",$cnt); $num = get_type_number($val); $txt = get_type_text($num); $npath = $show_path ? $path.'{"'.$key.'"}' : ""; $txt = $show_type_text ? $txt : ''; $out_text .= $key; if ($cnt > 1) { if ($add_top_jump) { $html_text .= "\nindex"; if ($add_alpha_list) { $key .= "\nalphabetic "; } $key .= "\nend"; $key .= "
\n"; $key .= "\nList of $icnt perl files as at "; $key .= get_YYYYMMDD(time()); $key .= ", generated by $pgmname"; $key .= "
\n"; $key .= "\ntop\n"; if ($add_alpha_list) { $txt .= "\nalphabetic "; } $txt .= "\nend "; $txt .= " |
top\n"; if ($add_alpha_list) { $txt .= "\nalphabetic "; } $txt .= "\nend "; $txt .= "
\n"; } $txt .= "\nList of $icnt perl files as at "; $txt .= get_YYYYMMDD(time()); $txt .= ", generated by $pgmname"; $txt .= "
\n"; $txt .= " \n"; $txt .= " \n"; $txt .= "\n"; rename_2_old_bak($out_html); write2file($key.$html_text.$txt,$out_html); prt("Summary written to $out_html\n"); } } sub process_file($$$) { my ($inf,$tm,$sz) = @_; if (! open INF, "<$inf") { pgm_exit(1,"ERROR: Unable to open file [$inf]\n"); } my @lines =