Wednesday, March 4, 2009

Validating data types in PERL

Since perl data types are very typeless and depends upon where they are used. There are some situations, when this design is awkward.

To validate data types,

Data::Types module can be installed and used.

However if you dont want to install, here are few quickies:

- To Check

% An Integer, Use regex /^[+-]?\d+$/ and ofcourse, not matched ones are non-intger
% A Decimal, /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/
% Whole number, /^\d+$/
% Float number, /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
%