in reply to IF condition with a range
TIMTOWTDI.
use Types::Common::Numeric 1.003_002 qw(IntRange); my $range = IntRange[ $y-1 => $y+1 ]; if ($range->check($x)) { ...; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: IF condition with a range
by afoken (Chancellor) on Jul 16, 2018 at 17:49 UTC | |
A Megabyte-sized package (uncompressed [Type-Tiny-1.002002.tar.gz), plus dependencies, plus a ton of objects generated just for a trivial range check? Isn't that a tiny bit too FAT? Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-) | [reply] [d/l] |
by tobyink (Canon) on Jul 17, 2018 at 08:33 UTC | |
The majority of the size of the distribution is the test suite. With nearly 1200 CPAN modules depending on Type::Tiny directly or indirectly, having a comprehensive test suite seems a good idea. Documentation and examples make up most of the remaining size. In terms of dependancies, the module Type::Tiny itself has no non-core dependancies (apart from a few modules that are bundled with it).
As you can see, they're all in core, except:
The module I recommended, Types::Common::Numeric does have one additional non-core dependancy: Exporter::Tiny. Exporter::Tiny was originally called Exporter::TypeTiny and bundled as part of the distribution too. However, I was convinced by chocolateboy that it had merit as a standalone module, so I split it out as a separate CPAN distribution. Seems he was right as it now has nearly 4000 downstream dependants. I don't think the fact that some of the modules bundled with Type::Tiny (but not Type::Tiny itself) require Exporter::Tiny makes it unworthy of the "tiny" name. Yes, if it's just one range check in a script, requiring any module whatsoever seems like overkill. But realistically, problems like this don't come up in isolation. Most non-trivial programs will need to sanity-check data in loads of places — checking the data passed to object constructors, checking function and method arguments, etc. Type::Tiny and its bundled modules provide a framework for this which is more concise and usually more correct and faster than handwritten code. As an example of correctness, spot the problem with this code:
Okay, so the obvious thing is we're not checking $age is a number. But if we do $object->set_age("old") we'll get a warning about doing a numeric comparison on a string, so we're covered, right? Wrong. $object->set_age([]) will be allowed. Numeric operations on references silently act on their reference address without triggering any warnings. Rewriting it to this:
And as a bonus, it will test that $self is an object, and will check that @_==2. None of the other answers to the OP's question even bothered checking $x was actually a number. IntRange will check that it's defined, it's a non-reference, and it's an integer before checking that it's in the right range. | [reply] [d/l] [select] |