Krish Krish - 7 months ago 13
Perl Question

Validate year range using Perl

These are the valid years in header of our source file, in copyright line:

2016
2007,2016
2007-2010,2016
2010,2012-2016


current year (2016) should be part of it.

invalid:

2016,2007 # not in order
2010-2007,2016 #range not in order.
2010-2015 # current year missing.


I made script as below, still working on it, is there a better way to check the same?

use strict;
use warnings;
use List::Util 'first';
use Time::Piece;

my $t = Time::Piece->new();
my $currentYear=$t->year;

my $filename="sample.txt"; # this file name will be passed-in to the script.
my $fileContent=`cat $filename`;
my $copyrightFound = first { /copyright .* Shakespeare/i } $fileContent;
if (!$copyrightFound) {
print "\nERROR: Copyright missing\n";
exit;
}
#copyright Found
print $fileContent;
if ($fileContent =~ /Copyright \(c\) (.*) by Bill Shakespeare\s+.*All rights reserved./) {
print "\nCopyright is good: $1\n";
my $years = $1;
print "\n$years, $currentYear\n";
#e.g: 2016
if ($years !~ ', ' && $years !~ '-') {
print "\nerror\n" if ($years !~ /$currentYear/);
}
#e.g: 2010-2016
elsif ($years !~ ', ') {
print "\nError\n" if ($years !~ /\-$currentYear$/);
}
#eg: 2010, 2016
elsif ($years !~ '-') {
print "\nError\n" if ($years !~ /\, $currentYear$/);
}
#e.g: 2008, 2010, 2011-2016
elsif ($years =~ '-' && $years =~ ', ') {
print "\nError 5\n" if ($years !~ /\d\d\d\d, \d\d\d\d\-$currentYear$/);
}
else {
print "invalid format"
}

} else {
print "\nCopyright needs to be fixed\n";
}


sample.txt has:

Copyright (c) 2008, 2010, 2011-2016 by Bill Shakespeare
All rights reserved.

Answer

You could use Set::IntSpan to validate the dates:

use warnings;
use strict;
use Set::IntSpan qw();

my $currentYear = 2016;

while (<DATA>) {
    s/\s+//g;
    my $ok = 0;
    if (Set::IntSpan->valid($_)) { # Checks order
        my $set = Set::IntSpan->new($_);
        my @years = $set->elements();
        $ok = 1 if grep { $_ == $currentYear } @years;
    }
    print "$ok : $_\n";
}

__DATA__
2007
2007, 2016
2007-2010,2016
2010,2012-2016
2016,2007
2010-2007,2016
2010-2015

This prints:

0 : 2007
1 : 2007,2016
1 : 2007-2010,2016
1 : 2010,2012-2016
0 : 2016,2007
0 : 2010-2007,2016
0 : 2010-2015

Simplifying using member instead of grep, as recommended by pilcrow in a comment:

my $ok = 0;
if (Set::IntSpan->valid($_)) { 
    my $set = Set::IntSpan->new($_);
    $ok = 1 if $set->member($currentYear);
}
print "$ok : $_\n";
Comments