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?)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.