I was wondering if anyone would be willing to critique my code. I'm learning Perl as my first programming language and I'm sure theres tons of things I could be doing better. My first (real) script is for the credit union I work at, it basically automates logging into a program we use and schedules a job with expect. I had originally written this in regular expect with some tcl but decided to rewrite it in perl.
use strict; use Expect; use POSIX; use Time::ParseDate; use Getopt::Std; my $command = 'connexid'; my $password = 'passwd'; my $login = 'loginid'; my $job = $ARGV[1]; my $queue = $ARGV[2]; my $date = $ARGV[3]; my $time = $ARGV[4]; my $exp; my $when; my %options=(); getopts("hvnlrt", \%options); if ($date) { date(); } if ($options{t}) { if ($job && $queue && $date && $time) { print "At $time on $when, this will run $job in queue $queue. \n"; } elsif ($job && $queue && $date) { print "Please enter a time for the job to run. \n"; } elsif ($job && $queue) { print "This will run $job in queue $queue immediately with the -n +flag \n"; } elsif ($job eq "install") { print "OK\n"; } else { print "Please specify a Job Name and Queue \n"; } } if ($options{h}) { help(); } if ($options{r}) { if ($date && $time) { repgen(); } elsif ($date) { die "Please specify a time for the job to run. \n"; } else { die "Please specify a date and time for the job to run. \n"; } } if ($options{l}) { if ($date && $time) { later(); } elsif ($date) { die "Please specify a time for the job to run. \n"; } else { die "Please specify a date and time for the job to run. \n"; } } if ($options{n}) { if ($job && $queue) { now(); } else { die "Please specify a job name and a queue. \n"; } } if ($options{v}) { print "autoepisys version 1.3\n"; } sub login{ if ($command eq "local") { $exp = Expect->spawn("sym") or die "Cannot spawn sym $!\n"; $exp->expect(10, '-re', 'UserId'); $exp->send("$login\r"); $exp->expect(10, '-re', 'Dedicate this console to this user?'); $exp->send("y\r"); } else { $exp = Expect->spawn($command) or die "Cannot spawn $command $!\n" +; $exp->expect(10, '-re', 'password:'); $exp->send("$password\r"); $exp->expect(10, '-re', '(#|\$) '); $exp->send("sym\r"); $exp->expect(10, '-re', 'UserId'); $exp->send("$login\r"); $exp->expect(10, '-re', 'Dedicate this console to this user?'); $exp->send("y\r"); } } sub logoff { if ($command eq "local") { $exp->expect(10, '-re', "Do you wish to log off the system?"); $exp->send("y\r"); $exp->soft_close(); } else { $exp->expect(10, '-re', "Do you wish to log off the system?"); $exp->send("y\r"); $exp->expect(10, '-re', '(#|\$) '); $exp->send("exit\r"); $exp->soft_close(); } } sub now { login(); $exp->expect(10, '-re', 'Menu Selection'); $exp->send("8\r"); $exp->expect(10, '-re', 'Menu Selection'); $exp->send("0\r"); $exp->expect(10, '-re', 'Selection'); $exp->send("0\r\r"); $exp->expect(10, '-re', 'Job File Name'); $exp->send("$job\r"); $exp->expect(10, '-re', 'Batch Options?'); $exp->send("Y\r"); $exp->expect(10, '-re', 'Display Batch Queues'); $exp->send("\r"); $exp->expect(10, '-re', 'Notify Upon Completion?'); $exp->send("\r"); $exp->expect(10, '-re', 'Queue Priority'); $exp->send("\r"); $exp->expect(10, '-re', 'Scheduled Start Date'); $exp->send("\r"); $exp->expect(10, '-re', 'Scheduled Start Time'); $exp->send("\r"); $exp->expect(10, '-re', 'Batch Queue'); $exp->send("$queue\r"); $exp->after(10, '-re', 'Exception Item Batch ID'); $exp->send("\r"); $exp->expect(10, '-re', 'Okay?'); $exp->send("Y\r"); $exp->expect(10, '-re', "Batch Job $job Is Done"); $exp->send("\e"); $exp->send("\e"); $exp->send("\e"); logoff(); } sub later { login(); $exp->expect(10, '-re', 'Menu Selection'); $exp->send("8\r"); $exp->expect(10, '-re', 'Menu Selection'); $exp->send("0\r"); $exp->expect(10, '-re', 'Selection'); $exp->send("0\r\r"); $exp->expect(10, '-re', 'Job File Name'); $exp->send("$job\r"); $exp->expect(10, '-re', 'Batch Options?'); $exp->send("Y\r"); $exp->expect(10, '-re', 'Display Batch Queues'); $exp->send("\r"); $exp->expect(10, '-re', 'Notify Upon Completion?'); $exp->send("\r"); $exp->expect(10, '-re', 'Queue Priority'); $exp->send("\r"); $exp->expect(10, '-re', 'Scheduled Start Date'); $exp->send("$when\r"); $exp->expect(10, '-re', 'Scheduled Start Time'); $exp->send("$time\r"); $exp->expect(10, '-re', 'Batch Queue'); $exp->send("$queue\r"); $exp->expect(10, '-re', 'Expected System Date'); $exp->send("$when\r"); $exp->expect(10, '-re', 'Expected Previous System Date'); $exp->send("\r"); $exp->after(10, '-re', 'Exception Item Batch ID'); $exp->send("$when\r"); $exp->expect(10, '-re', 'Okay?'); $exp->send("Y\r"); $exp->send("\e"); $exp->send("\e"); $exp->send("\e"); logoff(); } sub repgen { login(); $exp->expect(10, '-re', 'Menu Selection'); $exp->send("8\r"); $exp->expect(10, '-re', 'Menu Selection'); $exp->send("0\r"); $exp->expect(10, '-re', 'Selection'); $exp->send("1\r\r"); $exp->expect(10, '-re', 'Selection'); $exp->send("11\r"); $exp->expect(10, '-re', 'Specification File'); $exp->send("$job\r"); $exp->expect(10, '-re', 'Selected Run Date'); $exp->send("$when\r"); $exp->expect(10, '-re', "Specification File"); $exp->send("\r"); $exp->expect(10, '-re', "Batch Options?"); $exp->send("Y\r"); $exp->expect(10, '-re', 'Display Batch Queues'); $exp->send("\r"); $exp->expect(10, '-re', 'Notify Upon Completion?'); $exp->send("\r"); $exp->expect(10, '-re', 'Queue Priority'); $exp->send("\r"); $exp->expect(10, '-re', 'Scheduled Start Date'); $exp->send("\r"); $exp->expect(10, '-re', 'Scheduled Start Time'); $exp->send("$time\r"); $exp->expect(10, '-re', 'Batch Queue'); $exp->send("$queue\r"); $exp->expect(10, '-re', 'Okay?'); $exp->send("Y\r"); $exp->send("\e"); $exp->send("\e"); $exp->send("\e"); logoff(); } sub help { print "usage: \[-l\] \[-n\] \[-r\] \[-v\] \[-t\] JOB.NAME queue wh +en time\n"; print " The -l option sets the batch job to run later\n"; print " The -n option runs the batch job now (for script automa +tion)\n"; print " The -r option runs a report generator specification fil +e\n"; print " The -v option shows version information\n"; print " The -t option will show you what will happen with a que +ry\n"; print "Examples:\n"; print " auto -l NETTELLER 3 2days 0130\n"; print " auto -l ACH.MIDNIGHT.POST 2 tomorrow 0030\n"; print " auto -n DRAFT.POST 1\n"; print " auto -r DAILY.REFERENCE.FILE 1 today 1801\n"; print " auto -t SOME.COMMAND 2 3days 1801\n"; exit; } sub date { my $dt = parsedate($date); $when = strftime("%m%d%y", localtime($dt)); chomp($when); }

The code is also here on github: https://github.com/puntme/AutoEpisys/raw/master/epi Thanks in advance.


In reply to code critique by puntme

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.