Hi folks. I've got about 3 months experience with Perl under my belt from the U, and am trying my hand at arguments for the first time. At first, it seemed to be working fine, but using args is not mixing well with prompting for user input. Just wondering how I'm doing this wrong:
#!/usr/bin/perl
use warnings;
use strict;
use Date::Calc qw(
Today
Add_Delta_Days
);
# This program takes input from a data dump
# (CSV format) and produces the migration prep file
# with very little user input at prompt.
my $file = $ARGV[0];
my $num_args = $#ARGV + 1;
die "ERROR: Usage: prep_gen.pl subs_dump_file.csv\nPlease provide file
+ name\n" if ($num_args != 1);
#Confirm .csv format
my $period = rindex($file, ".");
die "Invalid file format; must be .csv extension.\n" if ($period == -1
+);
die "Invalid file format; must be .csv extension.\n" if (uc(substr($fi
+le, $period)) !~ /\.CSV$/);
my $fileName = substr($file, 0, $period);
open(my $infile, "<", $file) or die "Cannot open $file for reading!\n"
+;
open(my $outfile, ">", $fileName."-migration-prep.txt") or die "Cannot
+ open outfile for writing!\n"; # This will be the prep file create
+d
# We need the merchant name and customer ID
# This is the only prompted input
print "Please provide the full merchant name: \n";
my $merchName = <>;
chomp($merchName);
print $outfile "completeMerchantName=$merchName\n"; # Start adding
+content to outfile
my ($year, $month, $day) = Today(); # Calc today's date for nex
+t date calc
($year, $month, $day) = Add_Delta_Days($year,$month,$day,60); # Con
+clude date for migration will be +60 days
$month = sprintf("%02d", $month+1);
$day = sprintf("%02d", $day);
print "NOTE: migration conclude date set to $year-$month-$day 00:00:00
+\n";
print $outfile "cleanupDate=$year-$month-$day 00:00:00\n";
print "Please provide the customer ID as it appears in the DB: \n";
my $customerId = <>;
chomp($customerId);
while ($customerId !~ /^[0-9]+$/) { # Customer ID must be numer
+ic
print "Invalid input; customer ID must be completely numeric. Plea
+se try again: \n";
chomp($customerId = <>);
}
print $outfile "subscriptionDb.customerId=$customerId\n";
When I run this, I get a bunch of errors about uninitialized values, and it's not asking for some input like I need it to:
mmusser@localhost:~/programming/perl$ perl prep_gen.pl some_file.csv
Please provide the full merchant name:
NOTE: migration conclude date set to 2012-08-02 00:00:00
Please provide the customer ID as it appears in the DB:
Use of uninitialized value $customerId in scalar chomp at prep_gen.pl
+line 50, <> line 1.
Use of uninitialized value $customerId in pattern match (m//) at prep_
+gen.pl line 52, <> line 1.
Invalid input; customer ID must be completely numeric. Please try agai
+n:
However, if I don't use arguments and just prompt the user for a file name, everything works as I expect it to. Thanks in advance for any and all help!