/dev/urandom has asked for the wisdom of the Perl Monks concerning the following question:
And a Foo.pm module#!/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";
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?#!/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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Accessing lexically scoped variables from a subref
by chromatic (Archbishop) on Dec 28, 2007 at 00:23 UTC | |
by /dev/urandom (Beadle) on Dec 28, 2007 at 01:26 UTC | |
by benizi (Hermit) on Dec 31, 2007 at 04:41 UTC | |
|
Re: Accessing lexically scoped variables from a subref
by dragonchild (Archbishop) on Dec 28, 2007 at 04:34 UTC |