Commit 18a585ef authored by Graham Knop's avatar Graham Knop Committed by Karen Etheridge

allow 0 to be passed to native trait methods expecting strings

parent 0078c82a
......@@ -3,6 +3,12 @@ for, noteworthy changes.
{{$NEXT}}
[BUG FIXES]
- Various native trait methods would refuse "0" where they expected a
string. They have been fixed to allow all defined, non-reference values,
and all objects with string, number, or boolean overloads.
[DOCUMENTATION]
- Moose::Manual::Construction now notes that roles can modify the BUILD sub.
......
......@@ -6,7 +6,6 @@ use warnings;
use Module::Runtime 0.014 'use_package_optimistically', 'module_notional_filename';
use Data::OptList;
use Params::Util qw( _STRING );
use Sub::Exporter;
use Scalar::Util 'blessed';
use List::Util 1.33 qw(first any all);
......@@ -354,13 +353,9 @@ sub _load_user_class {
# XXX - this should be added to Params::Util
sub _STRINGLIKE0 ($) {
return 1 if _STRING( $_[0] );
if ( blessed $_[0] ) {
return overload::Method( $_[0], q{""} );
}
return 1 if defined $_[0] && $_[0] eq q{};
return 0 if !defined $_[0];
return 1 if !ref $_[0];
return 1 if overload::OverloadedStringify($_[0]);
return 0;
}
......
......@@ -118,6 +118,19 @@ use Test::Moose;
}
}
{
package OverloadNum;
use overload
q{0+} => sub { ${ $_[0] } },
fallback => 1;
sub new {
my $class = shift;
my $str = shift;
return bless \$str, $class;
}
}
{
subtest( 'simple case', sub { run_tests(build_class) } );
subtest(
......@@ -623,9 +636,24 @@ sub run_tests {
'join returns expected result when joining with empty string'
);
is(
$obj->join(0), '1020304',
'join returns expected result when joining with 0 as number'
);
is(
$obj->join("0"), '1020304',
'join returns expected result when joining with 0 as string'
);
is(
$obj->join( OverloadStr->new(q{}) ), '1234',
'join returns expected result when joining with empty string'
'join returns expected result when joining with object with string overload'
);
is(
$obj->join( OverloadNum->new(0) ), '1020304',
'join returns expected result when joining with object with numify overload'
);
like( exception { $obj->join }, qr/Cannot call join without at least 1 argument/, 'throws an error when passing no arguments to join' );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment