Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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?

In reply to Accessing lexically scoped variables from a subref by /dev/urandom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 20:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found