From: Vincent Pit Date: Thu, 26 Feb 2009 17:26:37 +0000 (+0100) Subject: Test and document constant as types X-Git-Tag: v0.03~11 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FLexical-Types.git;a=commitdiff_plain;h=99dd805ff2026836a638c45fed6ecd4d4d483833 Test and document constant as types --- diff --git a/MANIFEST b/MANIFEST index d556b64..9c5b8f6 100644 --- a/MANIFEST +++ b/MANIFEST @@ -11,6 +11,7 @@ t/10-args.t t/11-integrate.t t/12-padsv.t t/13-ro.t +t/14-constants.t t/20-object.t t/21-tie.t t/22-magic.t diff --git a/Makefile.PL b/Makefile.PL index 710fb1e..f25d9e7 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -13,6 +13,7 @@ my %META = ( build_requires => { 'ExtUtils::MakeMaker' => 0, 'Test::More' => 0, + 'constant' => 0, }, resources => { bugtracker => "http://rt.cpan.org/NoAuth/ReportBug.html?Queue=$dist", diff --git a/lib/Lexical/Types.pm b/lib/Lexical/Types.pm index f3ff14d..d97761b 100644 --- a/lib/Lexical/Types.pm +++ b/lib/Lexical/Types.pm @@ -165,8 +165,21 @@ You can integrate L in your module so that using it will provide =head1 CAVEATS -For C to be able to parse C, the package C must be defined somewhere, and this even if you use the C<'as'> option to redirect to another package. -It's unlikely to find a workaround, as this happens deep inside the lexer, far from the reach of an extension. +For C to be able to parse C, you need : + +=over 4 + +=item * + +either the C package to be defined ; + +=item * + +or for C to be a constant sub returning a valid defined package. + +=back + +Those restrictions apply even if you use the C<'as'> option to redirect to another package, and are unlikely to find a workaround as this happens deep inside the lexer - far from the reach of an extension. Only one mangler or prefix can be in use at the same time in a given scope. @@ -202,6 +215,8 @@ Tests code coverage report is available at L 6; + +use constant Str => 'MyTypes::Str'; +use constant Int => 'MyTypes::Int'; +use constant Num => 'MyTypes::Num'; + +sub MyTypes::Str::new { "str:$_[0]" } + +sub MyTypes::Int::new { "int:$_[0]" } + +{ package MyTypes::Num } + +{ + use Lexical::Types as => sub { $_[0] =~ /(?:Str|Int)/ ? ($_[0], 'new') : () }; + + my Str $x; + is $x, "str:MyTypes::Str", 'my constant_type $x'; + + my Int ($y, $z); + is $y, "int:MyTypes::Int", 'my constant_type ($y,'; + is $z, "int:MyTypes::Int", 'my constant_type $z)'; + + my Num $t; + is $t, undef, 'my constant_type_skipped $t'; + + my MyTypes::Str $u; + is $u, "str:MyTypes::Str", 'my MyTypes::Str $u'; + + my MyTypes::Num $v; + is $v, undef, 'my MyTypes::Num $v'; +}