Dear Monks,
I'm going to try my best not to pose an XY Problem here, but I'm having a tough time coming up with a succinct explanation. Here goes nothing.
I'm working on initialization code for a complex system, with many user- (programmer-)defined levels of nested data structures. The data structure is trivial for a computer to traverse, but making it comprehensible to the programmer is another story. Further, trusting the programmer to initialize it correctly using pure Perl variable syntax would ask for a lot of artificial housekeeping and invite errors, or worse, ambiguity. Fortunately, implementing just a few new simple keywords solves all of these problems (many of them right at compile time) with simple, concise syntax, but having those subs operate on anything less than a file-scoped variable has eluded me. Here's what I've tried/considered:
- Pure-OO, using method calls in place of prototyped subs, but this is one of those rare cases where the prototypes are most of the benefit of the expanded syntax.
- I wrote a proof-of-concept that uses caller to work on a variable right in the calling namespace, but that's far too likely to cause subtle bugs if someone tries to do anything remotely complex with the initialization code.
- Package-scoped variable: In many ways, this is even worse, as it's now essentially a per-thread global for any of the operations.
- Closures: Even though the closures can easily be passed a reference and even planted into $caller::, they suffer the same problem as #2.
- Source filters: So far the best option, although I'd feel much better finding a simple solution with a little symbol table and prototype hacking, if such a solution exists.
- Text parsing: Parsing a scalar (re-inventing Perl, inventing something even more arbitrary, or trying to stuff this problem into a well-known format), would definitely be an anti-pattern in this case, IMO.
Here's the sort of syntax I'm aiming for:
use The::Package;
# sub my_generator(&) is exported by The::Package
my $complex_result = my_generator {
foo 'bar';
takes_coderef { ... };
};
foo 'baz'; # Should not affect $complex_result.
# Ideally, should croak, or not even compile.
I hope this question makes sense, and has a solution which is much simpler than my explanation. The short, short version of my question is: is there any way to implement prototyped subs that will automatically gain access to a given scalar/ref/object/alias to the same without explicitly passing it in to every call or using the $arrow->notation?)
-
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.
|