Reputation: 347
I have dates say $date1, $date2, $date3.
I want to create array of these dates pass to subroutine & want to retrieve status of each date. Regular expression inside subroutine will evaluate date format.
I have create subroutine as DateValidator as,
my @newDateArray = qw /$date1, $date2, $date3/;
foreach (@newDateArray) {
print "Date used $_ : ";
DateValidator($_);
}
# Subroutine to evaluate dates
sub DateValidator {
my $dateVal=shift;
if ($dateVal =~ /^20?\d{2}\-0?(:?[1-9]|10|11|12)\-(\d{2})$/) {
if ($2 <= 31) {
print "All DD's are correct\n";
} else {
print "Please verify the DD again !\n";
}
} else {
print "Please enter correct date !\n";
}
}
This does not work as expected. Any help would be appreciated.
Upvotes: 0
Views: 95
Reputation: 67908
The qw()
function does not interpolate variables. So this code:
my @newDateArray = qw /$date1, $date2, $date3/;
Needs to be:
my @newDateArray = ($date1, $date2, $date3);
I.e. replace qw()
with a simple pair of parentheses.
This is not explicitly mentioned in the documentation, but there is a rather subtle mention:
Evaluates to a list of the words extracted out of STRING, using embedded whitespace as the word delimiters. It can be understood as being roughly equivalent to:
split(" ", q/STRING/);
Where the observant people will notice that a single quoted STRING -- using q()
-- will not interpolate variables. This could have been written quite a few hundred times clearer, I agree.
Also, you might notice that the documentation says:
A common mistake is to try to separate the words with comma or to put comments into a multi-line qw-string. For this reason, the use warnings pragma and the -w switch (that is, the $^W variable) produces warnings if the STRING contains the "," or the "#" character.
Which you have not noticed, which makes me suspect that you are not using warnings. This is a very, very bad idea. See Why use strict and warnings? for more information.
Upvotes: 4