Binding

 view release on metacpan or  search on metacpan

lib/Binding.pm  view on Meta::CPAN

    die "Unknown var: $varname";
}

sub my_vars {
    my ($self) = @_;
    my $vars = peek_my($self->{level});
    return $vars;
}

sub our_vars {
    my ($self) = @_;
    my $vars = peek_our($self->{level});
    return $vars;
}


1;
__END__

=head1 NAME

Binding - eval with variable binding of caller stacks.

=head1 VERSION

This document describes Binding version 0.01

=head1 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...

=head1 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.

=head1 INTERFACE

=over

=item of_caller([ $level ])

One of the constructors. The C<$level> parameter is optional and
defaults to 1, which means one level up in the stack. The returned
value is a object of C<Binding> class, which can latter be invoked
with C<eval> method.

=item eval( $code_str )

An instance method that evals code in C<$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;
    }

=item var( $name )

Return the value of the variable named $name in the specified scope.

=item my_vars

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 C<peek_my> function in L<PadWalker>.

=item our_vars

Returns all variables declared with "our" that's visible in the given
binding.

Returns a hashref, which keys are variable names and values are
references to variable values.

See C<peek_our> function in L<PadWalker>.

=back

=head1 DEPENDENCIES

L<PadWalker>, L<Data::Dump>, L<Devel::Caller>


=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to
C<bug-binding@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.



( run in 2.415 seconds using v1.01-cache-2.11-cpan-8f98c5d2c55 )