Restating this, because there is a bit of confusion of what I want to do, and to put it more in context. Note that I'm thinking more about simplifying the end-user's side as opposed to simplicitiy in the module code at this point. Please note that this is for a personal project, so if it can't be done, I'll figure out something.

An instance of MyPackage will have a function that can be used to pass in numerous coderefs defined by the user. The MyPackage instance will retain a list of these coderefs as an array, but this detail should be unknown to the user. When the user calls a second function on the instance of MyPackage (with no arguments), the function will select a certain coderef from the array (mostly at random), and execute that coderef. So far, so good; this is easy to do.

However, I would like to provide some support functions that work on an instance of MyPackage, some which might need to know which coderef has been used. I know that since the user already knows the instance of MyPackage, they can use that to run this function, but the additional arguments to it can get problematic. From a UI point of view, they should not know about these extra arguments, only because if they call the functions with the wrong arguments, they may mess up the workings of the package.

So conceptually, I'd like it to look like this:

--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; my $extra_arg = $some_variable; return &$coderef( ); } sub helper { my $self = shift; my $extra_arg = 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"; helper(); } ); # HERE'S THE RUB

or, in other words, I want the call to "helper" in "execute()" to have arguements filled it for it without the user filling them in for himself, such that "helper" would be called with arguments ($self, $other_arg).

Now that I write it down this way, I'm wondering if I can use @_ to make this work; I know that @_ behaves in some weird ways with subroutines, but I'll take a look at how that happens to see. But it should be noted that the user coderefs, from the user's standpoint, should not be expecting any arguments.

The other option that I could go with is to use eval'd strings instead of coderefs; thus I could catch for the text "helper" and regex the variables that I need into it when the user sets this. The only thing I don't like about this is that you lose compile time syntax checking of the user's code.


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 Re: Hiding, but maintaining variables in user-defined code by Masem
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.