Masem has asked for the wisdom of the Perl Monks concerning the following question:
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(); } );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hiding, but maintaining variables in user-defined code
by jeroenes (Priest) on May 23, 2001 at 10:08 UTC | |
by bwana147 (Pilgrim) on May 23, 2001 at 11:44 UTC | |
|
Re: Hiding, but maintaining variables in user-defined code
by Masem (Monsignor) on May 23, 2001 at 17:20 UTC | |
by jeroenes (Priest) on May 23, 2001 at 17:49 UTC | |
by Masem (Monsignor) on May 24, 2001 at 17:12 UTC | |
by Anonymous Monk on May 23, 2001 at 18:53 UTC | |
|
Re: Hiding, but maintaining variables in user-defined code
by gumpu (Friar) on May 23, 2001 at 10:00 UTC | |
|
Re: Hiding, but maintaining variables in user-defined code
by Vynce (Friar) on May 23, 2001 at 15:20 UTC |