Subject: | "Str" TypeConstraint does not allow string overloaded objects |
The "Str" type constraint will not allow string overloaded objects.
This is a problem as string overloading can be used to hide from the
user that the scalar in question it an object, the user won't know
otherwise until their Moose object throws a type violation. For
example, a function may document that it returns a URI string which is
actually a URI object. Class::DBI documents it returns column names but
returns string overload column objects.
This becomes a compatibility problem when a function which formerly
returned a string starts returning a string overloaded object to
maintain compatibility. A Moose object with a Str type would break that
compatibility.
I've worked around it by defining a type like so:
subtype 'NotEmptyStr'
=> as 'Defined',
=> where sub {
return if ref($_) and !overload::Overloaded($_, q[""]);
return length $_ > 0;
};
Here's a simple example using URI as a string overloaded object.
{
package Foo;
use URI;
use Moose;
has string => (
is => 'rw',
isa => 'Str',
);
Foo->new->string("foo");
Foo->new->string(URI->new);
}