raghvens has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am trying to use getopt in my script for the first time, but the options are not working, got confused where exactly I need to use sub modules for the options. Output expected is to run the build time depending upon the options provided like unix, win, and all for all platforms. Now the script is not working if the option -win or -unix is provided, but works fine if all the options like -unix -win -all are provided. Appreciate your help, Kindly suggest.

: # Runs Perl scripts via sh on all Progress platforms including NT/MKS. eval '\ if [ -f /tools/etc/setmach ]; then\ . /tools/etc/setmach;\ fi;\ if [ -z "$PROCESSOR_LEVEL" ]; then\ exec /tools/$MACHINE/perl-5.6.1/bin/perl $perldebug -S $0 ${1+"$@"}; +\ else\ export SHELL=$ROOTDIR/mksnt/sh.exe;\ perl $perldebug -S $0 ${1+"$@"};\ exit $?;\ fi' if 0; ###################################################################### +########## $| = 1; use strict; use Cwd 'abs_path'; use lib '/scratch/rsiddapp/perl_modules/Carp-Clan-6.04/lib'; use lib '/scratch/rsiddapp/perl_modules/Date-Calc/lib'; use Date::Calc qw(:all); use Getopt::Long; use Pod::Usage; my @ERRORS; my %OPTION; print " + DEBUG : Calling ParseCommandLineOptions\n"; ParseCommandLineOptions(@_); sub SystemCmd(@_) { my @cmd = @_; my $pwd = `pwd`; if (system(@_)) { LogErrorAndExit("system cmd @_ failed in $pwd"); } } sub LogErrorAndExit(@_) { print "ERROR: @_\n"; exit 1; } #print "\nPlease Enter For which OSTORE version you want the b +uild time, E.g. 7.4.0 \n"; my $ver; #--------------------------------------------------------------------- +---------- # Parse command line arguments and fill up 'OPTIONS' hash #--------------------------------------------------------------------- +---------- sub ParseCommandLineOptions() { print " + DEBUG : Getting the Options\n"; GetOptions(\%OPTION, 'all', 'win', 'unix'); foreach my $pKey ( sort keys %OPTION ) { print " ++ DEBUG : *$pKey* \n"; } if ( $OPTION{all}) { if ($OPTION{unix}) { print "\nPlease Enter For which OSTORE version you want the bu +ild time, E.g. 7.4.0 \n"; $ver = <STDIN>; chomp($ver); my $path = "/odi/ostore_platform_logs/ostore/$ver/test1"; chdir($path); opendir (LOC, "/odi/ostore_platform_logs/ostore/ver/test1") || die "ca +n't opendir $!\n"; my @list = readdir(LOC); foreach my $d (@list) { next if ( $d =~ /^(\.+|all$)/ ); chomp($d); if ($d =~ /^winnt/) { next; } push (my @plat,$d); print "\nPlatform Name:: $d"; foreach my $p (@plat) { $p =~ s/\.$//; chomp($p); my $npath = '/odi/ostore_platform_logs/ostore/7.4.0/test1'; $npath = $npath.'/'. $p; chdir($npath); #print "\n$npath"; my $LATEST = `ls -tr | tail -1`; chomp $LATEST; print "\n################################\n"; my $pFile = $path .'/'. $p .'/'. $LATEST . '/'. 'build_os. +log'; open ( FH, "<$pFile") || die " = ERROR : Problem reading the file : $p +File : $!\n"; my $ghModule; my $pOpen = 0; my $pStart = ''; my $pEnd = ''; my $pModule = ''; while ( my $pLine = <FH> ) { chomp $pLine; # Ignore lines of the below format # Starting stop OSCI tslaunch in ostore_7.4_in +t_bld.sol2c5.picopt at 2011-09-10-1738 next if ( $pLine =~ /^\s*Starting\s+stop\s+/ ) +; if ( ( $pOpen ) && ( $pLine =~ /\s*:\s*Complet +ed\s+/ ) ) { $pEnd = $pLine; $pEnd =~ s/.*\s+at\s+//; $pEnd =~ s/\.$//; $ghModule->{$pModule}{START} = $pStart; $ghModule->{$pModule}{END} = $pEnd; $pOpen = 0; $pEnd = ''; $pStart = ''; } if ( $pLine =~ /^\s*Starting.*\s+at\s+/ ) { next if ( $pLine =~ /^\s*Starting\s+javac\s+/ +); $pOpen = 1; $pModule = $pLine; $pModule =~ s/^\s*Starting//i; $pModule =~ s/\s+at.*$//i; # Starting osci cdimage in ostore_7.4_int_bld. +sol2c5.picopt at 2011-09-08-1725. $pStart = $pLine; $pStart =~ s/.*\s+at\s+//; $pStart =~ s/\.$//; } } close FH; foreach my $pModule ( sort keys %{$ghModule} ) { my $pTime = &getDifference($ghModule->{$pModul +e}{START}, $ghModule->{$pModule}{END} ); print " + $pModule .. $ghModule->{$pModule}{ST +ART} .. $ghModule->{$pModule}{END} .. $pTime\n"; } # sub getDifference { my $pTime1 = shift || ''; my $pTime2 = shift || ''; my $pTimeConv1 = &getRelativeTime($pTime1); my $pTimeConv2 = &getRelativeTime($pTime2); #my $pTimeDiff = $pTimeConv2 - $pTimeConv1; ################################################ my $pTimeDiff = ($pTimeConv2 - $pTimeConv1)/60; return "$pTimeDiff minutes"; ################################################ #return "$pTimeDiff seconds"; } sub getRelativeTime { my $pValue = shift || ''; $pValue =~ /(\d+)-(\d{2})-(\d{2})-(\d{4})/; my $pYear = $1; my $pMonth= $2; my $pDay = $3; my $pTime = $4; $pTime =~ /(\d{2})(\d{2})/; my $pHour = $1; my $pMin = $2; # Leap my $pLeapYear = 0; $pLeapYear = 1 if ( $pYear%4 == 0 ); my $pYearConv = &getElapsedYear($pYear); my $pMonthConv = &getElapsedMonth($pMonth, $pLeapYear); my $pDayConv = $pDay*24*60*60; my $pHourConv = $pHour * 60 * 60 ; my $pMinConv = $pMin * 60; my $pTotalConv = $pYearConv + $pMonthConv + $pDayConv + $pHourConv + + $pMinConv; return $pTotalConv; } sub getElapsedMonth { # my $pMonth = shift || ''; my $pLeapYear = shift || 0; my $pTime = 0; $pMonth =~ s/^0//; while ( $pMonth >= 1 ) { if ( $pMonth =~ /1|3|5|7|8|10|12/ ) { $pTime += 31*24*60*60; } elsif ( $pMonth =~ /4|6|9|11/ ) { $pTime += 30*24*60*60; } elsif ( $pMonth eq '2' ) { if ($pLeapYear) { $pTime += 29*24*60*60; } else { $pTime += 28*24*60*60; } } $pMonth--; } return $pTime; } sub getElapsedYear { my $pYear = shift || ''; my $stYear = 2010; # my $pTime = 0; while ( $pYear > $stYear ) { if ( $pYear%4 == 0 ) { $pTime += 366*24*60*60; } else { $pTime += 365*24*60*60; } $pYear--; } return $pTime; } print "\n################################\n"; } } } if ( $OPTION{win} ) { print " ++DEBUG :: In the Windows Routine\n"; my $bm_file='/scratch/rsiddapp/diva_script/build_modul +es'; my @build_module=(); my @start_check_module=(); my @finish_check_module=(); my $wm_file='/scratch/rsiddapp/diva_script/windows_mac +hines'; my @windows_machines=(); my ($sdate,$time,$fdate,$ftime)=""; my $log_path="/odi/ostore_platform_logs/ostore/$ver/te +st1"; my $log_file='osi_nt_driver.log'; my $sdate; my $stime; my $fdate; my $ftime; read_build_module_input(); read_windows_machine_input(); get_start_finish_time(); sub read_build_module_input { open(BM,"$bm_file") || die "Couldn't open the +file $bm_file $!\n"; while (my $bm_inp=<BM>) { chomp($bm_inp); push(@build_module,$bm_inp); } close(BM); } sub read_windows_machine_input { open(WM,"$wm_file") || die "Couldn't open the +file $wm_file $!\n"; while(my $wm_inp=<WM>) { chomp($wm_inp); push(@windows_machines,$wm_inp); } close(WM); } sub get_start_finish_time { foreach my $mac (@windows_machines) {print "\n\n***************$mac******* +***********\n"; foreach my $mod (@build_module +) {print "\nMODULE: $mod +\n"; my $dir=`ls -t +r ${log_path}/${mac} | tail -1`; chomp($dir); open(LF,"${log +_path}/${mac}/${dir}/${log_file}") || die "Couldn't open the file $lo +g_file $! \n"; while (my $lin +e=<LF>) { chomp( +$line); if ($l +ine =~ /Started\s+$mod/ || $line =~ /Finished\s+$mod/) { + if ($line =~ /Started/) + { + if (!grep(/$mod/,@start_check_module)) + { + push(@start_check_module,$line); + $line =~ s/\*//g; + (my $sdate, my $stime)=split (/\s+/,$line); + } + } + if ($line =~ /Finished/) + { + if (!grep(/$mod/,@finish_check_module)) + { + $line =~ s/\*//g; + (my $fdate,my $ftime)=split(/\s+/,$line); + } + } } } close(LF);@sta +rt_check_module=@finish_check_module=(); if ($sdate && +$stime && $fdate && $ftime) { time_diff($sda +te,$stime,$fdate,$ftime); $sdate=$stime= +$fdate=$ftime=""; } else { next; } }print "\n************ +**End of $mac**********\n"; } } sub time_diff() { my ($tsdate,$tstime,$tfdate,$tftime)=@_; my ($tsyear,$tsmon,$tsday)=split(/-/,$tsdate); my ($tshh,$tsmm,$tsss)=split(/:/,$tstime); my ($tfyear,$tfmon,$tfday)=split(/-/,$tfdate); my ($tfhh,$tfmm,$tfss)=split(/:/,$tftime); my ($tot_day,$tot_hh,$tot_mm,$tot_ss)=Delta_DH +MS($tsyear,$tsmon,$tsday,$tshh,$tsmm,$tsss,$tfyear,$tfmon,$tfday,$tfh +h,$tfmm,$tfss); my $total_hour=24*$tot_day+$tot_hh; print "Run Start Date: $tsdate\t Run Start Tim +e: $tstime\n"; print "Run End Date: $tfdate\t Run End Time: $ +tftime\n"; print "Total Time Taken: $total_hour:$tot_mm:$ +tot_ss \n"; } } } }
Currently I am getting this output:

When –win is given as option, we get the following output: ./test.pl -win + DEBUG : Calling ParseCommandLineOptions + DEBUG : Getting the Options ++ DEBUG : *win*

When –unix is given as option, we get the following output: ./test.pl -unix + DEBUG : Calling ParseCommandLineOptions + DEBUG : Getting the Options ++ DEBUG : *unix*

When –all is given as option we get the following: ./test.pl -all + DEBUG : Calling ParseCommandLineOptions + DEBUG : Getting the Options ++ DEBUG : *all*

When the same script used with all the options, output is as expected. ./test.pl -all -unix -win + DEBUG : Calling ParseCommandLineOptions + DEBUG : Getting the Options ++ DEBUG : *all* ++ DEBUG : *unix* ++ DEBUG : *win*

Replies are listed 'Best First'.
Re: options in perl
by Anonymous Monk on Nov 04, 2011 at 09:29 UTC