Binding - eval with variable binding of caller stacks.
This document describes Binding version 0.01
use Binding; sub inc_x { my $b = Binding->of_caller; $b->eval('$x + 1'); } sub fortytwo { my $x = 41; inc_x; } sub two { my $x = 1; inc_x; } # You probably get the idea now...
This module can help when you need to eval code with caller's variable binding. It's similar to Tcl's uplevel function. The name comes from the Binding class of Ruby language.
It's not doing much yet but let you grab caller variables.
One of the constructors. The $level parameter is optional and defaults to 1, which means one level up in the stack. The returned value is a object of Binding class, which can latter be invoked with eval method.
$level
Binding
eval
An instance method that evals code in $code_str. Block form of eval is not accepted. Variables used in $code_str will be referenced to the one lives the given caller frame.
$code_str
# calculate $x + 5 sub x_add_five { # notice the single quotes here. Binding->of_caller->eval('$x + 5'); } { my $x = 3; my $y = add_five; }
Return the value of the variable named $name in the specified scope.
Returns all variables declared with "my" in the given binding.
Returns a hashref, which keys are variable names and values are references to variable values.
See peek_my function in PadWalker.
peek_my
Returns all variables declared with "our" that's visible in the given binding.
See peek_our function in PadWalker.
peek_our
PadWalker, Data::Dump, Devel::Caller
No bugs have been reported.
Please report any bugs or feature requests to bug-binding@rt.cpan.org, or through the web interface at http://rt.cpan.org.
bug-binding@rt.cpan.org
The returned object of Binding->of_caller need to be used immediately, or at least in the same frame of its construction, which means that this doesn't do what it means yet:
Binding->of_caller
sub add { my $x = 5; my $b = Binding->of_caller; # Expect the binding '$x' is to the one in caller of add() add_x($b); } sub add_x { my $binding = shift; # But this $x is referring to the one in add() $binding->eval('$x + 1') } my $x = 3; add; # returns 6 instead of 4;
The standard Binding class in Ruby core: http://www.ruby-doc.org/core/classes/Binding.html, and the extended Binding class http://extensions.rubyforge.org/rdoc/classes/Binding.html.
Kang-min Liu <gugod@gugod.org>
<gugod@gugod.org>
Copyright (c) 2008, Kang-min Liu <gugod@gugod.org>.
This is free software, licensed under:
The MIT (X11) License
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
To install Binding, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Binding
CPAN shell
perl -MCPAN -e shell install Binding
For more information on module installation, please visit the detailed CPAN module installation guide.