0

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.

TLP
  • 66,756
  • 10
  • 92
  • 149
Singham
  • 71
  • 1
  • 6
  • 2
    **What does "doesn't work" mean?** "Doesn't work" is an inadequate description for us to understand the problem. What happened when you tried it? Did you get an error message? Did you get incorrect results? Did you get *no* results? If the results were incorrect, what made them incorrect? What were you expecting instead? Did you get *any* correct results? If so, what were they? Don't make us guess. – Andy Lester Mar 19 '14 at 14:57
  • 2
    You cannot use `qw()` with variables, because it does not interpolate them. Is that what "does not work"? Note that you have not given us any input to test your subroutine with, so how are we supposed to test it? – TLP Mar 19 '14 at 15:00
  • Also, you should use proper indentation, it makes your code much easier to read. I have formatted your code for you now. – TLP Mar 19 '14 at 15:01
  • Inside subroutine DateValidator() $_ is not interpolated. so I am getting incorrect results. – Singham Mar 19 '14 at 15:01
  • @Singham No, inside `qw /$date1, $date2, $date3/`, `$date1`, `$date2` and `$date3` are not interpolated. – TLP Mar 19 '14 at 15:02
  • Is there any alternative available to this issue? – Singham Mar 19 '14 at 15:04
  • I tried @newDateArray = qw/\$date1, \$date2, \$date3/ – Singham Mar 19 '14 at 15:06
  • @Singham I have answered your question. And no, escaping the dollar sign `$` won't work. That's what you use to *prevent* interpolation. – TLP Mar 19 '14 at 15:13
  • Downvoted as `use warnings` would have given strong hints to the problem. – Dave Cross Mar 19 '14 at 16:15
  • 1
    @DaveCross Actually, it would only have warned about the commas, not about the lack of interpolation. Still, writing code without `use warnings` is like russian roulette: Most of the time it looks like its perfectly safe, but once in a while it blows your head off. – TLP Mar 19 '14 at 16:21

1 Answers1

4

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.

Community
  • 1
  • 1
TLP
  • 66,756
  • 10
  • 92
  • 149
  • 2
    You're welcome. Yes, making mistakes is a good way to learn. Which is why you should let `use warnings` tell you about your mistakes. :) – TLP Mar 19 '14 at 15:32