Permalink
Browse files

Added documentation for string caching

  • Loading branch information...
upasana-me committed May 13, 2013
1 parent 729546e commit e5dd0c177ce26d29217ab23c32a4452aa8919b17
Showing with 17 additions and 10 deletions.
  1. +14 −10 lib/Moose/Util/TypeConstraints/Builtins.pm
  2. +3 −0 t/attributes/numeric_defaults.t
@@ -80,19 +80,23 @@ sub define_builtins {
subtype 'Num'
=> as 'Str'
=> where {
- /\A[+-]?[0-9]+\z/ ||
- /\A(?:[+-]?) #matches optional +- in the beginning
- (?=[0-9]|\.[0-9]) #matches previous +- only if there is something like 3 or .3
- [0-9]* #matches 0-9 zero or more times
- (?:\.[0-9]+)? #matches optional .89 or nothing
- (?:[Ee](?:[+-]?[0-9]+))? #matches E1 or e1 or e-1 or e+1 etc
- \z/x;
+ my $val = $_;
+ ($val =~ /\A[+-]?[0-9]+\z/) ||
+ ( $val =~ /\A(?:[+-]?) #matches optional +- in the beginning
+ (?=[0-9]|\.[0-9]) #matches previous +- only if there is something like 3 or .3
+ [0-9]* #matches 0-9 zero or more times
+ (?:\.[0-9]+)? #matches optional .89 or nothing
+ (?:[Ee](?:[+-]?[0-9]+))? #matches E1 or e1 or e-1 or e+1 etc
+ \z/x );
}
=> inline_as {
# the long Str tests are redundant here
- $value_type->_inline_check($_[1])
- .' && ( '.$_[1].'=~ /\A[+-]?[0-9]+\z/ || '
- . $_[1].' =~ /\A(?:[+-]?) #matches optional +- in the beginning
+ 'my $val = '.$_[1].';'. #storing $_[1] in a temporary value,
+ #so that $_[1] won't get converted to a string for regex match
+ #see t/attributes/numeric_defaults.t for more details
+ $value_type->_inline_check('$val')
+ .' && ( $val =~ /\A[+-]?[0-9]+\z/ || '
+ . '$val =~ /\A(?:[+-]?) #matches optional +- in the beginning
(?=[0-9]|\.[0-9]) #matches previous +- only if there is something like 3 or .3
[0-9]* #matches 0-9 zero or more times
(?:\.[0-9]+)? #matches optional .89 or nothing
@@ -121,6 +121,9 @@ with_immutable {
my $b = B::svref_2object(\$val);
my $flags = $b->FLAGS;
ok($flags & B::SVf_NOK || $flags & B::SVp_NOK, "it's a num");
+ # it's making sure that the Num value doesn't get converted to a string for regex matching
+ # this is the reason for using a temporary variable, $val for regex matching,
+ # instead of $_[1] in Num implementation in lib/Moose/Util/TypeConstraints/Builtins.pm
ok(!($flags & B::SVf_POK), "not a string");
}
} 'Baz2';

0 comments on commit e5dd0c1

Please sign in to comment.