Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Verifying a date

by choroba (Bishop)
on Oct 02, 2019 at 15:38 UTC ( #11106970=note: print w/replies, xml ) Need Help??

in reply to Verifying a date

String eval is considered a bad practice. Block eval is safe, and if you don't like it, use any of the try/catch modules available for a more mainstream look.

#!/usr/bin/perl use warnings; use strict; use Time::Piece; sub validate_date { my ($date) = @_; return eval { $date eq Time::Piece->strptime($date, '%m.%d.%y')->s +trftime('%m.%d.%y') } } use Test::More tests => 5; ok validate_date('02.28.19'); ok ! validate_date('02.32.19'); ok ! validate_date('02.29.19'); ok validate_date('02.29.16'); ok ! validate_date('02.30.16');

The eq is needed because 02.29.19 is parsed without an error by Time::Piece, returning 03.01.19 when formatted back to a string.

Update: It passes Corion's test cases correctly.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11106970]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2019-11-15 10:12 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (83 votes). Check out past polls.