NAME
Binding - eval with variable binding of caller stacks.
VERSION
This document describes Binding version 0.01
SYNOPSIS
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...
DESCRIPTION
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.
INTERFACE
- of_caller([ $level ])
-
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 ofBinding
class, which can latter be invoked witheval
method. - eval( $code_str )
-
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.# 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; }
- var( $name )
-
Return the value of the variable named $name in the specified scope.
DEPENDENCIES
PadWalker, Data::Dump, Devel::Caller
BUGS AND LIMITATIONS
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.
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:
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;
SEE ALSO
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.
AUTHOR
Kang-min Liu <gugod@gugod.org>
LICENCE AND COPYRIGHT
Copyright (c) 2008, Kang-min Liu <gugod@gugod.org>
.
This is free software, licensed under:
The MIT (X11) License
DISCLAIMER OF WARRANTY
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.