X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=README;h=bba2ba0abf2a26a97a95b0b3d9f78a1f47250147;hb=ef454c52e92ecd149b3bf0f6e221162cad3955ac;hp=6e144adbf134a68e99aa61b747b519abacab7fe6;hpb=06cb3f3506161a118dc440264dcabcd612b9432c;p=perl%2Fmodules%2FLexical-Types.git diff --git a/README b/README index 6e144ad..bba2ba0 100644 --- a/README +++ b/README @@ -2,22 +2,36 @@ NAME Lexical::Types - Extend the semantics of typed lexicals. VERSION - Version 0.01 + Version 0.03 SYNOPSIS + { package Str; } + + { + package My::Types::Str; + + sub new { bless { }, shift } + } + + use Lexical::Types as => sub { 'My::Types::' . $_[0] => 'new' }; + + my Str $x; # $x is now a My::Types::Str object + { - package Str; + package My::Types::Int; - sub TYPEDSCALAR { Some::String::Implementation->new } + sub TYPEDSCALAR { bless { }, shift } } use Lexical::Types; - my Str $x; # $x is now a Some::String::Implementation object + use constant Int => 'My::Types::Int'; + + my Int $y; # $y is now a My::Types::Int object DESCRIPTION - This module allows you to hook the execution of typed lexicals - declarations ("my Foo $x"). In particular, it can be used to + This pragma allows you to hook the execution of typed lexicals + declarations ("my Str $x"). In particular, it can be used to automatically tie or bless typed lexicals. It is not implemented with a source filter. @@ -25,38 +39,47 @@ DESCRIPTION FUNCTIONS "import [ as => [ $prefix | $mangler ] ]" Magically called when writing "use Lexical::Types". All the occurences - of "my Foo $x" in the current lexical scope will be changed to call at + of "my Str $x" in the current lexical scope will be changed to call at each run a given method in a given package. The method and package are - determined by the parameter "as" : + determined by the parameter 'as' : - * If it's left unspecified, the "TYPEDSCALAR" method in the "Foo" + * If it's left unspecified, the "TYPEDSCALAR" method in the "Str" package will be called. use Lexical::Types; my Str $x; # calls Str->TYPEDSCALAR * If a plain scalar $prefix is passed as the value, the "TYPEDSCALAR" - method in the "${prefix}::Foo" package will be used. + method in the "${prefix}::Str" package will be used. use Lexical::Types as => 'My::'; # or "as => 'My'" my Str $x; # calls My::Str->TYPEDSCALAR * If the value given is a code reference $mangler, it will be called - at compile-time with arguments 'Foo' and 'TYPEDSCALAR' and is - expected to return the desired package and method name (in that - order). If any of those is "undef", the default value will be used - instead. + at compile-time with arguments 'Str' and 'TYPEDSCALAR' and is + expected to return : - use Lexical::Types as => sub { 'My', 'new_' . lc($_[0]) }; - my Str $x; # the coderef indicates to call My->new_str + * either an empty list, in which case the current typed lexical + definition will be skipped (thus it won't be altered to trigger + a run-time hook) ; + + use Lexical::Types as => sub { return $_[0] =~ /Str/ ? @_ : () }; + my Str $y; # calls Str->TYPEDSCALAR + my Int $x; # nothing special + + * or the desired package and method name, in that order (if any of + those is "undef", the default value will be used instead). + + use Lexical::Types as => sub { 'My', 'new_' . lc($_[0]) }; + my Str $x; # the coderef indicates to call My->new_str The initializer method receives an alias to the pad entry of $x in $_[1] - and the original type name ("Foo") in $_[2]. You can either edit $_[1] + and the original type name ("Str") in $_[2]. You can either edit $_[1] in place, in which case you should return an empty list, or return a new scalar that will be copied into $x. "unimport" - Magically called when writing "no Lexical::Types". Turns the module off. + Magically called when writing "no Lexical::Types". Turns the pragma off. INTEGRATION You can integrate Lexical::Types in your module so that using it will @@ -83,10 +106,15 @@ INTEGRATION sub new_int { ... } CAVEATS - For "perl" to be able to parse "my Foo $x", the package "Foo" must be - defined somewhere, and this even if you use the "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 "perl" to be able to parse "my Str $x", you need : + + * either the "Str" package to be defined ; + + * or for "Str" to be a constant sub returning a valid defined package. + + Those restrictions apply even if you use the '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. @@ -116,9 +144,14 @@ SUPPORT perldoc Lexical::Types + Tests code coverage report is available at + . + ACKNOWLEDGEMENTS Inspired by Ricardo Signes. + Thanks Florian Ragwitz for suggesting the use of constants for types. + COPYRIGHT & LICENSE Copyright 2009 Vincent Pit, all rights reserved.