Lexical::Types - Extend the semantics of typed lexicals.
VERSION
- Version 0.01
+ Version 0.02
SYNOPSIS
{
DESCRIPTION
This module allows you to hook the execution of typed lexicals
- declarations ("my Foo $x"). In particular, it can be used to
+ 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.
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 $x; # nothing special
+ my Int $y; # calls Int->TYPEDSCALAR
+
+ * 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.
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
+ For "perl" to be able to parse "my Str $x", the package "Str" 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.