I have the following code that parses an input file and outputs a .stat file and a .csv file. I would like to pass the following input parameters:

-i input filename (instead of having it hard-coded) -s output .stat file (to a certain location) -o output .csv file (to a certain location)

I have not worked with input parameters before. What is the best way of setting this up? Thank you

# This program parses a error_log for necessary information and output +s a CSV file with the highest busy value for each 5 minute interval. + # The program also outputs a STAT file with the last 5 minute interval + of the CSV. use strict; use warnings; # Ignore theses values my %ignorables = map { $_ => 1 } qw([notice mpmstats: rdy bsy rd wr ka + log dns cls bsy: in); # Open the error log - Change the name of the file as needed. open my $error_fh, '<', 'iset_error_log'; # Subroutine to pull the line containing AP22,SM22, and Apache stats sub findLines { my($item,@result)=(""); # Iterates over the lines in the file, putting each into $_ while (<$error_fh>) { # Select only those fields that have the word 'notice' if (/\[notice/) { # Place those lines with the word 'rdy' on the next line if (/\brdy\b/){ push @result,"$item\n"; $item=""; } else { $item.=","; } # Split the line into fields, separated by spaces, skip th +e %ignorables my @line = grep { not defined $ignorables{$_} } split /\s+ +/; # More cleanup s/|^\[|notice|[]]//g for @line; # remove unnecessary eleme +nts from the array # Output the line. @line = join(",", @line); s/,,/,/g for @line; map $item.=$_, @line; } } @result } # Place the subroutine contents into an array my @array = &findLines; my $line; # Create an subroutine to place the contents of AP22, SM22, and Apache + in the correct order sub Program{ my @return = (); chomp @array; my ($dow,$mon,$day,$time,$year,$rdy,$bsy,$rd,$wr,$ka,$log,$dns,$cl +s,$dow2,$mon2,$day2,$time2,$year2,$val1,$mod1,$val2,$mod2,$val3,$mod3 +,$ap22,$sm22,$apache); foreach $line (@array){ ($dow,$mon,$day,$time,$year,$rdy,$bsy,$rd,$wr,$ka,$log,$dns,$cls,$ +dow2,$mon2,$day2,$time2,$year2,$val1,$mod1,$val2,$mod2,$val3,$mod3) = + ((split /[,]/, $line),("")x24); $line = "$dow,$mon,$day,$time,$year,$rdy,$bsy,$rd,$wr,$ka,$log,$dn +s,$cls"; # For lines with no variables if ($mod1 eq ""){ $line = $line.","."0".","."0".","."0"; } # For lines with only SM22 if ($mod1 eq "mod_sm22.cpp" && $mod2 eq ""){ $line = $line.",".$val1.","."0".","."0"; } # For lines with only AP22 if ($mod1 eq "mod_was_ap22_http.c" && $mod2 eq "" && $mod3 eq ""){ $line = $line.",".$val1.","."0".","."0"; } # For lines with AP22-SM22-Apache if ($mod1 eq "mod_was_ap22_http.c" && $mod2 eq "mod_sm22.cpp" && $ +mod3 eq "ApacheModule.cpp"){ $line = $line.",".$val1.",".$val2.",".$val3; } # For lines with SM22-AP22-Apache if ($mod1 eq "mod_sm22.cpp" && $mod2 eq "mod_was_ap22_http.c" && $ +mod3 eq "ApacheModule.cpp"){ $line = $line.",".$val2.",".$val1.",".$val3; } # For lines with AP22-SM22 if ($mod1 eq "mod_was_ap22_http.c" && $mod2 eq "mod_sm22.cpp" && $ +mod3 eq ""){ $line = $line.",".$val1.",".$val2.","."0"; } # For lines with SM22-AP22 if ($mod1 eq "mod_sm22.cpp" && $mod2 eq "mod_was_ap22_http.c" && $ +mod3 eq ""){ $line = $line.",".$val2.",".$val1.","."0"; } # For lines with SM22-Apache if ($mod1 eq "mod_sm22.cpp" && $mod2 eq "ApacheModule.cpp" && $mod +3 eq ""){ $line = $line.","."0".",".$val2.",".$val2; } # For lines with AP22-Apache if ($mod1 eq "mod_was_ap22_http.c" && $mod2 eq "ApacheModule.cpp" +&& $mod3 eq ""){ $line = $line.",".$val1.","."0".",".$val2; } # Push the array out of the subroutine ($dow,$mon,$day,$time,$year,$rdy,$bsy,$rd,$wr,$ka,$log,$dns,$cls,$ +ap22,$sm22,$apache) = ((split/[,]/, $line),("")x16); push @return, ("$line\n"); } return @return; } # Initialize the hashes my %interval; my %month; @month{qw/ jan feb mar apr may jun jul aug sep oct nov dec +/} = '01' .. '12'; # Put the contents of the subroutine into an array my @finalArray = &Program; # Delete the first line of the array $finalArray[0] = ""; # Create a new array without the first line my @lastArray = @finalArray; # Initialize CSV file and STAT file and print the header for the CSV f +ile open my $fh, ">", 'log_5min.csv' or die $!; print $fh "Time Interval,rdy,bsy,rd,wr,ka,log,dns,cls,ap22,sm22,apache +\n"; open my $FILE, ">", 'last_5min.stat' or die $!; # Select only those lines with the highest busy count in each 5 minute + interval my @maxima; for my $record (@lastArray) { my @fields = $record =~ /([^,\s]+)/g; next unless @fields; my @range = @fields[1..4]; $range[2] =~ s|(\d+):\d\d$|5*int($1/5)|e; my $range = join ' ', @range; my $value = $fields[5]; my ($dow, $mon, $day, $time, $year, $rdy, $by, $rd, $wr, $ka, $log +, $dns, $cls, $ap22, $sm22, $apache) = split /[,]/, $record; my $record2 = "$range,$rdy,$by,$rd,$wr,$ka,$log,$dns,$cls,$ap22,$s +m22,$apache"; if (@maxima == 0 or $range ne $maxima[-1][0]) { push @maxima, [$range, $value, $record2]; } else { @{$maxima[-1]}[1,2] = ($value, $record2) if $maxima[-1][1] > $ +value; } } # Print the contents to the CSV file print $fh $_->[2] for @maxima; # Print the last line to the STAT file my $string = (); $string = $_->[2] for $maxima[-1]; my ($timeStamp, $rdy, $by, $rd, $wr, $ka, $log, $dns, $cls, $ap22, $sm +22, $apache) = split /[,]/, $string; print $FILE "<ED>$by</ED>\n<ECO>Max Busy</ECO>\n<ECE></ECE>";

In reply to Perl Input Parameters for Unix by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.