Recalling the other thread that seems to have inspired this, I expect that you will encounter a few problems:
- Each with body will need its own auto-generated package for each class of object you want as "context": ref $OBJ should be sufficient to choose a context package.
- The Perl prototype functionality that allows a keyword sub to be omitted only works for the first argument, if I remember correctly, so this may force you to change the syntax a bit or use a source filter.
- The closure you get for the with body has already been compiled in its surrounding package, so you will need XS magic to crack it open and relocate it into the auto-generated package for the context. It may be easiest to simply move the closure into ref $OBJ and let Perl handle the rest, but watch for with being used in the scope of a use vars — you will need to translate references to globals that came from use vars. Globals declared using our are actually lexical aliases, so they should be carried with the closure when you move it to another package.
I have only a few readings of perlguts for how any of this is actually implemented, so I could be wrong here, but with appears fairly difficult to "bolt-on" to Perl. I expect a JS-like with to need to be added to the core language to work, or to hook sufficiently far into the interpreter that it may as well have been added to the core language.