in reply to ensure - Ensure that the environment is good.
You should enable the warnings and strict pragmas at the beginning of your code.
#!/usr/bin/perl use warnings; use strict;
7 use subs qw( 8 error_exit 9 usage 10 precheck 11 generic_check 12 perl_module_check 13 print_ok 14 print_notok 15 print_info 16 print_verbose 17 );
You don't really need to use the subs pragma. The usual way to predeclare subs is to use the sub function:
sub error_exit; sub usage; sub precheck; sub generic_check; sub perl_module_check; sub print_ok; sub print_notok; sub print_info; sub print_verbose;
21 @Default_Tools_List = ( 22 [ 'gzip' ,'app', '' , 'generic_check' ], 23 [ 'gunzip' ,'app', '' , 'generic_check' ], 24 [ 'tar', ,'app', '' , 'generic_check' ], 25 [ 'tr', ,'app', '' , 'generic_check' ], 26 [ 'date' ,'app', '' , 'generic_check' ], 27 [ 'ps', ,'app', '' , 'generic_check' ], 28 [ 'cat', ,'app', '' , 'generic_check' ], 29 [ 'id', ,'app', '' , 'generic_check' ], 30 [ 'echo' ,'app', '' , 'generic_check' ], 31 [ 'pwd', ,'app', '' , 'generic_check' ], 32 [ 'chmod' ,'app', '' , 'generic_check' ], 33 [ 'ln', ,'app', '' , 'generic_check' ], 34 [ 'cp', ,'app', '' , 'generic_check' ], 35 [ 'rpm', ,'app', '' , 'generic_check' ], 36 [ 'clear' ,'app', '' , 'generic_check' ], 37 [ 'dirname' ,'app', '' , 'generic_check' ], 38 [ 'df' ,'app', '' , 'generic_check' ], 39 [ 'Carp' ,'perlm', 'Perl Module - Carp', 'perl_modul +e_check' ], 40 ) ; 41 42 43 $Tools_Hash = \@Default_Tools_List ;
You are only using @Default_Tools_List twice. You can assign the array reference directly to $Tools_Hash. You should also use code references instead of strings for the subroutines. (Also $Tools_Hash is an array not a hash.)
my $Tools_Hash = [ [ 'gzip' ,'app', '' , \&generic_check ], [ 'gunzip' ,'app', '' , \&generic_check ], [ 'tar' ,'app', '' , \&generic_check ], [ 'tr' ,'app', '' , \&generic_check ], [ 'date' ,'app', '' , \&generic_check ], [ 'ps' ,'app', '' , \&generic_check ], [ 'cat' ,'app', '' , \&generic_check ], [ 'id' ,'app', '' , \&generic_check ], [ 'echo' ,'app', '' , \&generic_check ], [ 'pwd' ,'app', '' , \&generic_check ], [ 'chmod' ,'app', '' , \&generic_check ], [ 'ln' ,'app', '' , \&generic_check ], [ 'cp' ,'app', '' , \&generic_check ], [ 'rpm' ,'app', '' , \&generic_check ], [ 'clear' ,'app', '' , \&generic_check ], [ 'dirname' ,'app', '' , \&generic_check ], [ 'df' ,'app', '' , \&generic_check ], [ 'Carp' ,'perlm', 'Perl Module - Carp', \&perl_module_check + ], ];
59 %precheck_stuff = ($KSH, $PERL); 220 for $ent (@precheck_stuff) { 221 if( ! -f $ent ) { 222 print_info "$ent not found\n" ; 223 $ret = 1 if($ret == 0); 224 } 225 }
The hash %precheck_stuff is never used after you assign to it. The array @precheck_stuff is always empty.
93 my @more_paths = split(/\s+/, $more_paths); 113 my @extra_tools_tochk = split(/\s+/, $extra_tools); 128 push (@extra_tools_tochk, split(/\s+/, $l)); 138 my @exclude_checks = split(/\s+/, $exclude_list); 146 my @implicit_checks = split(/\s+/, $implicit_list);
You probably want to use my @array = split ' ', $variable instead so that $array[ 0 ] will always be non-empty.
92 chomp($more_paths); 112 chomp($extra_tools); 121 chomp($tools_from_file); 137 chomp($exclude_list); 145 chomp($implicit_list);
These variables do not need to be chomped unless the user is doing something really weird on the command line.
115 push(@{$Tools_Hash}, [ $elem, 'app', $elem, 'generic_ch +eck']); 131 push(@{$Tools_Hash}, [ $elem, 'app', $elem, 'generic_ch +eck']); 154 push(@{$Tools_Hash}, [ $elem, 'app', $elem, 'generic_c +heck']);
Again, the fourth element should be a code reference instead of a string.
175 foreach $component ( @{$Tools_Hash} ) { 176 my $entity = $component->[0] ; # first thing is the enti +ty to check. 177 if($things_to_check{$entity} == 1 ) { 178 $check_function = $component->[-1] ; # last thing is the +function to check. 179 $ret = \&$check_function($entity, splice(@{$component},1, +-1)); #rest of the inbetween things are args to function. 180 } 181 }
I would use shift for $entity and pop for $check_function so you don't have to use splice in the function call.
for my $component ( @$Tools_Hash ) { my $entity = shift @$component; # first thing is the entity to ch +eck. if ( $things_to_check{ $entity } == 1 ) { my $check_function = pop @$component; # last thing is the func +tion to check. my $ret = $check_function->( $entity, @$component ); #rest of +the inbetween things are args to function. } }
240 print_info sprintf "%-${cf_flen}s", $show_user ; 282 print_info sprintf "%-${cf_flen}s", $show_user ;
You shouldn't use string interpolation in sprintf format strings.
print_info sprintf '%-*s', $cf_flen, $show_user ;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: ensure - Ensure that the environment is good.
by muralikrishnan (Novice) on Sep 19, 2007 at 04:49 UTC |