|
|
| laziness, impatience, and hubris | |
| PerlMonks |
Catching error in DateTime::Formatby Anonymous Monk |
| on Oct 27, 2025 at 17:29 UTC ( [id://11166601]=perlquestion: print w/replies, xml ) | Need Help?? |
|
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: I'm an inexperienced Perl programmer (my background is mainly in Python), charged with keeping a legacy app running, and I'm having trouble figuring out how to validate a date. (At least, that's what I think the problem is.) We have a function that takes a date value and does some transformations on it. This is thoroughly tested and works fine for its use case, but we don't actually have a test for what happens if someone sends garbage into it—it assumes the input really is a valid date. Normally, this is fine; the input comes from internal sources and should always be OK. We now need to extend it for a case when we don't have control over the incoming data. So, I tried to wrap it in an eval to catch any errors. Currently, it looks like this: The test script now has this: When I run this, however, I get (I've changed the file paths):
So the test "works" in that it returns an "ok" value, but it also prints the whole "Invalid date format" thing. How do I get it to _not_ do this, or otherwise test to see if the input is valid before I pass it to DateTime::Format::ISO8601->parse_datetime? The docs for this function say "The parse_datetime method will attempt to match these ambiguous strings" etc., but doesn't say what happens if it can't, or what you're supposed to do to test for valid input. I tried to write a test script, but even though it looks identical (to me), this does _not_ print the "Invalid date format":
Running this simply prints "ERROR" and that's it, no other output. What's going on in the real code, or what's the right way to handle this?
Back to
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||