Masem, maybe I miss something here. But, by the very definition of OOP, the user already knows what $self is: It is the very object that (s)he is calling. In this case, $test. What else do you want?

For globals and the like, just store everything in the $self={} hash, unless they are package globals. In either case, keep the interface clean by not letting the user access the $self hash and the globals directly. Use methods for that instead. So if you have a $self->{evil_case}->{unit666}=0, I would write a method $self->IsDevilPresent() that returns the zero. Your user code would than be:

$test->execute( { print "This is user code\n"; die "The devil is in $test->name()! I can't live with that!" if $test->IsDevilPresent(); } );
So in general, hide the implementation from the user. Keep the interface clean and the subroutines independent.

Hope this helps,

Jeroen
"We are not alone"(FZ)
Update: On the bike to work, my lightbulb went on (ask tilly about it ;-). You don't want to call the $test from the sub, but you want the dynamic $self from the package. So the code becomes:

$test->execute( { my $self = shift; print "This is user code\n"; die "The devil is in $self->name()! I can't live with that!" if $self->IsDevilPresent(); } ); #In the package, you must supply $self as an arg: return &$coderef( $self );

In reply to Re: Hiding, but maintaining variables in user-defined code by jeroenes
in thread 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.