1. Don't need parens when using "my" to declare a single variable - e.g.

    my $confINPUT;

    not:

    my($confINPUT);

    Similarly, do

    my ($confID, $confPIN)

    instead of:

    (my($confID), my($confPIN))
  2. Don't escape characters that don't need escaping, like the underscores at line ~33.
  3. You're inconsistent with operations that you check for failure. AFAIK it's pointless to check close() for failure when you've opened a file read-only. On the other hand, you aren't checking the result of your print() calls, which could fail.
  4. "my $confINPUT" at approx. line 24 (file scope) is unused (shadowed by the same-named variable inside the while loop) - eliminate it.
  5. Don't double-quote things that don't need string interpolation, like "$ARGV[0]". Just write $ARGV[0] directly.
  6. Some of your comments don't match the code, like # close files for input and # open files for output.
  7. Be aware that terminating your "die" messages with a newlines tells perl to not print the line number where the die occurs. This may be desirable if you want simpler error messages for end-users, but for debugging it's usually nice to see the line number.
  8. You copy and unlink the input file while you still have an open file handle (CONF_ID) to it. *nix lets you do this, but it won't work on Windows.
  9. You should check the result of the File::Copy copy() call. If the copy fails in your current code, you still blow away the original input file.
I'd refactor to something like this:
#!/usr/bin/perl # Written By Jon # This script takes CONF_IN and populates # 3 templates which are then put into a final file all # together # Usage: perl conf_add.pl # requires: # input: templates in templates folder # input: list of conference IDs and conference PINs # with ID and PIN on one line in order. # program outputs to file: conference_additions # # TODO: Error handling when there are no entries in CONF_ID # TODO: SCP files to server # TODO: Random Number Generator # TODO: Add insert.pl use strict; use warnings; use File::Copy ("cp"); use POSIX; my $TEMPLATES_DIR = "templates"; my $FINISHED_DIR = "finished_list"; my $OUTPUT_DIR = "additions"; my $date = POSIX::strftime('%d_%b', localtime); open(my $confid_fh, "<", $ARGV[0]) || die("$!\n"); my $i = 0; while (my $line = <$confid_fh>) { $i++; my ($confID, $confPIN) = split(" ", $line); process_template("conf_template1", "conf_list1.tmp", { XXXXXX => $ +confID }); process_template("conf_template2", "conf_list2.tmp", { XXXXXX => $ +confID, AAAA => $confPIN }); process_template("conf_template3", "conf_list3.tmp", { XXXXXX => $ +confID }); } close $confid_fh; open(my $confadd_fh, ">>", $OUTPUT_DIR . "/additions_$ARGV[0]_$date") +|| die("$!\n"); # adds 1st set of conf config to conf_additions print $confadd_fh "; start conference config 1\n"; append("conf_list1.tmp", $confadd_fh); # adds 2nd set of conf config to conf_additions print $confadd_fh "\n\n; start conference config 2\n"; append("conf_list2.tmp", $confadd_fh); # adds 3rd set of conf config to conf_additions print $confadd_fh "\n\n; meetme_additional_custom.conf\n"; append("conf_list3.tmp", $confadd_fh); close $confadd_fh; # moves list to finished folder cp($ARGV[0], $FINISHED_DIR . "/$ARGV[0].finished"); unlink $ARGV[0]; # deletes temporary files unlink "conf_list1.tmp" || die("$!\n"); unlink "conf_list2.tmp" || die("$!\n"); unlink "conf_list3.tmp" || die("$!\n"); print "Number of Entries processed: $i\n"; exit 0; sub process_template { my ($tmplfile, $outfile, $substitutions_ref) = @_; open(my $out_fh, ">>", $outfile) || die("$!\n"); open(my $template_fh, "<", $TEMPLATES_DIR . "/" . $tmplfile) || di +e("$!\n"); while (my $line = <$template_fh>) { while (my ($key, $value) = each %{$substitutions_ref}) { $_ =~ s/$key/$value/g; } print $out_fh $_; } close $template_fh; close $out_fh || die("$!\n"); } sub append { my ($infile, $out_fh) = @_; open(my $in_fh, "<", $infile) || die("$!\n"); while (<$in_fh>) { print $out_fh $_; } close $in_fh; }

In reply to Re: Error Correction by amedico
in thread Error Correction by PyrexKidd

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.