Module::AnyEvent::Helper::PPI::Transform - PPI::Transform subclass for AnyEvent-ize helper
version v0.0.5
Typically, this module is not used directly but used via Module::AnyEvent::Helper::Filter. Of course, however, you can use this module directly.
my $trans = Module::AnyEvent::Helper::PPI::Transform->new( -remove_func => [qw()], -translate_func => [qw()] ); $trans->file('Input.pm' => 'Output.pm');
NOTE that this module itself does not touch package name.
There are some helper functions can be exported.
use Module::AnyEvent::Helper::PPI::Transform qw(function_name); function_name($element); # returns function name whose definition includes the element # or you can call them as class methods Module::AnyEvent::Helper::PPI::Transform->function_name($element);
To make some modules AnyEvent-frinedly, it might be necessary to write boiler-plate codes. This module applys the following transformations.
Emit use AnyEvent;use Module::AnyEvent::Helper; at the beginning of the document.
use AnyEvent;use Module::AnyEvent::Helper;
Translate (ordinary) methods to _async methods.
Emit my $___cv___ = AE::cv; at the beginning of the methods.
my $___cv___ = AE::cv;
Emit return $___cv___; at the end of the methods.
return $___cv___;
Replace method calls with pairs of Module::AnyEvent::Helper::bind_scalar and shift->recv.
Module::AnyEvent::Helper::bind_scalar
shift->recv
Delete methods you need to implement by yourself.
Create blocking wait methods from _async methods to emit Module::AnyEvent::Helper::strip_async_all();1; at the end of the packages.
Module::AnyEvent::Helper::strip_async_all();1;
Additionally, this module inherits all of PPI::Transform methods.
Furthermore, there are some helper functions. It might be helpful for implementing additional transformer of Module::AnyEvent::Helper::Filter.
-remove_func
Specify array reference of removing methods. If you want to implement async version of the methods, you specify them in this option.
-translate_func
Specify array reference of translating methods. You don't need to implement async version of these methods. This module translates implementation.
-replace_func
Specify array reference of replacing methods. It is expected that async version is implemented elsewhere.
-delete_func
Specify array reference of deleting methods. If you want to implement not async version of the methods, you specify them in this option.
This module inherits all of PPI::Transform methods.
All functions described here can be exported and can be called as class methods.
# The followings are identical Module::AnyEvent::Helper::PPI::Transform::function_name($word); Module::AnyEvent::Helper::PPI::Transform->function_name($word);
function_name($element)
$element MUST be PPI::Element object. If $element is included in a function definition, its function name is returned. Otherwise, undef is returned.
$element
undef
is_function_declaration($word)
$word MUST be PPI::Token::Word object. If $word points a function name of a function declaration, true is returned. Otherwise, false is returned.
$word
delete_function_declaration($word)
$word MUST be PPI::Token::Word object and SHOULD be is_function_declaration is true. Delete the function declaration from the document.
copy_children($prev, $next, $target)
$prev specifies the where elements are inserted after. $next specifies the where elements are inserted before. One of the two MUST be valied PPI::Element object. If both are valid, the first paramter is used and the second parameter is ignored.
$prev
$next
$target specifies PPI::Element holding elements inserted at the place specified by $prev or $next.
$target
emit_cv($block)
$block is PPI::Structure::Block object.
$block
is inserted at the beginning of the block, and
is inserted at the end of the block.
emit_cv_into_function($word)
$word is PPI::Token::Word object and SHOULD be is_function_declaration is true. emit_cv is called for the block of the function declaration.
emit_cv
replace_as_async($element, $name, $is_array)
$element is a PPI::Element object and SHOULD point the name of the function call. $name is the function name that will be set as the contents of $element. $is_array is a boolean flag spcifying whether returning context is list context or not.
$name
$is_array
If the first argument points at call in the following code:
call
my $var = 42 + $some->[$idx]{$key}->call($arg1, $arg2) * func(); # codes follows ...
after the call of this function with replace_as_async($elem, 'call_async', 0), converted into as follows:
replace_as_async($elem, 'call_async', 0)
Module::AnyEvent::Helper::bind_scalar($___cv___, call_async($arg1, $arg2), sub { my $var = 42 + shift->recv() * func(); # codes follows ... });
For the case of replace_as_async($elem, 'call_async', 1), converted into as follows:
replace_as_async($elem, 'call_async', 1)
Module::AnyEvent::Helper::bind_array($___cv___, call_async($arg1, $arg2), sub { my $var = 42 + shift->recv() * func(); # codes follows ... });
See also Module::AnyEvent::Helper.
Yasutaka ATARASHI <yakex@cpan.org>
This software is copyright (c) 2012 by Yasutaka ATARASHI.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Module::AnyEvent::Helper, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Module::AnyEvent::Helper
CPAN shell
perl -MCPAN -e shell install Module::AnyEvent::Helper
For more information on module installation, please visit the detailed CPAN module installation guide.