tags:

views:

171

answers:

4

I need to convert a date to a string. The date is entered as 07/04/2010 and should then read July 4th 2010. It should also be able to be entered using singe digits instead of double (7 instead of 07, and it needs to add the 20 to the year if the user enters only /10).

This is what I have so far -

#!/usr/bin/perl
use CGI qw(:standard);
use strict;

#declare variables
my ($date, $month, $day, $year);
my @months = (  "January", "February", "March"
              , "April", "May", "June", "July"
              , "August", "September", "October"
              , "November", "December"
             ); 

#assign input item to variable
$date = param('Date');

#break date apart
$date =~ /([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,2}|20[0-9]{2,2})/;
$month = $1;
$day = $2;
$year = $3;
unless($year =~ /20[0-9]{2,2}/){
    $year = "20".$year;
}
$date = $months[int($1)]." ".$day.", ".$year;

#display date
print "<HTML><HEAD><TITLE>The Date</TITLE></HEAD>\n";
print "<BODY>\n";
print "The date is: $date\n";
print "</BODY></HTML>\n";

However I keep getting errors

Use of uninitialized value in pattern match (m//) at c08ex6.cgi line 14.
Use of uninitialized value in pattern match (m//) at c08ex6.cgi line 18.
Use of uninitialized value in concatenation (.) or string at c08ex6.cgi line 19.
Use of uninitialized value in int at c08ex6.cgi line 21.
Use of uninitialized value in concatenation (.) or string at c08ex6.cgi line 21.
A: 

You are getting these warning messages because the value of $date does not match your regular expression. Try to print the value of $date:

use CGI qw(:standard); 
use strict; 
use warnings;

#declare variables 
my ($date, $month, $day, $year); 
my @months = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");  

#assign input item to variable 
$date = param('Date'); 
print "date >>>$date<<<\n";

It is also a good practice to check if your regex matches before trying to use the captured values.

#break date apart 
if ($date =~ /([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,2}|20[0-9]{2,2})/) {
    $month = $1; 
    $day = $2; 
    $year = $3; 
}
else {
    die "Error: unexpected date format: $date";
}
toolic
+4  A: 

Don't reinvent the wheel if perfectly round wheels are already available to you. :-) Use a module such as DateTime or Time::Piece.

For instance ...

#!/usr/bin/perl

use strict;
use warnings;

use CGI qw(:standard);
use Time::Piece;

my $date = param('Date');
my $t = Time::Piece->strptime($date, '%m/%d/%Y');
$date = $t->strftime('%B %e, %Y');

#display date
print "<HTML><HEAD><TITLE>The Date</TITLE></HEAD>\n";
print "<BODY>\n";
print "The date is: $date\n";
print "</BODY></HTML>\n";

– Michael

mscha
If that was an option, I would - but this is for an assignment so I can't - it has to be done this way. I just don't understand what I am doing wrong. Form A takes a date and passes it to this script, and this is supposed to convert it into a string as I explained. If I put a date in instead of doing param, it works fine - but if I put that exact same date in the form and send it.. I get errors
Jason
@Jason, that means that you aren't having a problem converting the date. You're having a problem getting the date from the HTML form into your script. You're probably using the wrong parameter name.
cjm
Well, your first error means that $date is not defined. And because of that, pretty much every other variable you use is undefined as well.In the first place, add some checks to your code, like: my $date = param('Date') or die "Parameter 'Date' missing";Then, figure out why parameter 'Date' is missing.
mscha
+1  A: 

This works as expected:

#!/usr/bin/perl
use strict;

#declare variables
my ($date, $month, $day, $year);
my @months = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 

#assign input item to variable
#$date = param('Date');


#break date apart
while(<DATA>) {
   $date=$_;
   $date =~ /^\s*([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})\s*$/;
   $month = int($1-1);
   $day = $2;
   $year = $3;
   $year="20$year" if ($year=~/^\d\d$/);
   $date = $months[$month]." ".$day.", ".$year;

   print "date=$date\n";
}

__DATA__
7/4/1999    
   07/4/2010
1/1/2011
12/31/2010
4/4/09

Output:

date=July 4, 1999
date=July 4, 2010
date=January 1, 2011
date=December 31, 2010
date=April 4, 2009

I am guessing that the issue is getting the data from the form.

drewk
+1  A: 

Your Perl code is somewhat dodgy (you don't check if your regular expression match succeeded before using the results from it) but the first warning you are getting should give the problem away. You are using an uninitialized value in your regular expression.

That should tell you that $date is undefined. That implies that your input (either form or query parameter) does not have a parameter called Date in it. Try fixing that and then fix all the problems in your code mentioned by the other people answering you.

Nic Gibson