This question is somewhat related to one I asked earlier, however, I don't think the same solution would be as effective here as elsewhere.

In this case, I have a class package; one of the functions of this class will take as an arguement user code. However, the user code may call functions that are part of this class package; these functions, unforunately, would require to know what the $self variable is as well as possibly other variables from the $self class. In the previous case, I decided on passing a hash containing variables that the user can have, but in this case, some of the variables should stay private, so giving them directly to the user is not a good idea.

I have no idea if it's possible to do what I want to do at all; I might have to play with globals at the package level to make it work right, but there's possibilities of running this in a threaded environment, which would make the use of such globals unreliable.

Here's some sample code to indicate what I'm aiming for. I want the helper() function to have a way to get $self from execute() as well as other variables without letting the user know about them in their coderef.

--in MyPackage.pm #!/usr/bin/perl -w package MyPackage; use strict; sub new { my ( $class ) = @_; my $self = {}; bless $self, $class; return $self; } sub execute { my $self = shift; my $coderef = shift; my $s = $self; return &$coderef( ); } sub helper { my $self = shift; print "in helper\n"; } 1; --in MyTest.pl #!/usr/bin/perl -w use strict; use MyPackage; my $test = MyPackage->new(); $test->execute( sub { print "in the local coderef\n"; $test->helper(); } );

Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

In reply to Hiding, but maintaining variables in user-defined code by Masem

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.