http://www.perlmonks.org?node_id=659268

/dev/urandom has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks, Lets say I have a subref at my disposal, and the code in that subref includes a bunch of variables, that have been defined in the same block as the subref. Is it possible for me to access these variables somehow? I have the following code to illustrate my question correctly: A foo.pl file
#!/usr/bin/perl -w use strict; use Foo; my $i = 1; my $bar = 'foo'; my $foo = Foo->new; print "|", $foo->convert(sub {$i; return $bar}), "|\n";
And a Foo.pm module
#!/bin/false package Foo; use Devel::Peek; sub new { return bless {}, shift}; sub convert { shift; use B::Deparse; my $c = shift; my $b = B::Deparse->new("-d"); my @p = split /\n/, $b->coderef2text($c); print "FOOBAR: " . Dump($c) . " :BARFOO\n"; return $p[2] . '::' . eval $p[2]; } 1;
In this example, I want to get the value of the variables, that are used in the subref which is passed to the method of Foo (in this case, $i and $bar). Since the module is in a different file, I can't access the the variable ($i) content using eval. However, if I actually run the subref, it will return the correct value, so I think that in the subref's scope, these variables can be accessed. Is it possible for me to access these variables, maybe using some sort of B magic?