http://www.perlmonks.org?node_id=157300

tlhf has asked for the wisdom of the Perl Monks concerning the following question:

Recently, I've been writing a very high level VBScript to Perl compiler, or a 'translator'. By recently, I mean over the last year or so, with odd bits of hacking on it when I felt like it. Most of the problems I've faced have fallen, but there is one I've been putting off till now, because it scared me.


Let's look at the VBScript code;
option explicit dim x() x = split("john,biran,someone",",") response.write(x(0) & x(2))
The parser sees the option explicit, and realises that that means use strict. It sees the dim x(), and realises that that means my(@x). It understands that x is an array, and the parser always treats a solitry x as @x. And it goes forth, so the translation comes out roughly as;
use strict; my (@x); @x = b_split('john,biran,someone',','); $response->write(@x[0] . @x[2]); sub b_split { # not fully finished, needs $count and $compare my ($string, $delimiter, $count, $compare) = @_; $delimiter = quotemeta($delimiter); return split(/$delimiter/, $string); }
The @x[0] isn't clean, but it will do for now. This code works fine, and all is well. Well; all is well when we have wonderous option explicit. Unfortunately, there are many naughty VBScript developers who don't use option explicit. They don't define their variables. And my module gets very confused.

Without the option explicit and the dim x(), my translator outputs:
$x = b_split('john,biran,someone',','); print($x[0]; . $x[2]); sub b_split { # not fully finished, needs $count and $compare my ($string, $delimiter, $count, $compare) = @_; $delimiter = quotemeta($delimiter); return split(/$delimiter/, $string); }
Which is all kinds of wrong. So, I started my quest for the solution.

My first thought was to try to figure out what x would be by looking forward. Only that's a silly thought, because x could be redefined dynamically, to maybe a string or an integer rather than an array.

My second thought was to try to work out what it would be via a perl function. Something like C<context("x") = b_split('john,biran,someone',',');>. But this did not work either, for I knew now way of acheiving such a feat. Until someone on IRC pointed me towards the wonderus lvalue. Yet even after playing with it, lvalue reaped no rewards. It seemed to have little provision for the arrays and lists, and the context() seemed to execute before the lvalue passing occured.

I was going too play with some of the deepest perl magics I know of, the illustrius tie, when I ran accross this url. It seems to achieve what I want to do, yet I know not of how to use it. And I'm not sure if it does achieve anything similar to what I want to do. I feel like a lost little boy.

Has anyone got any ideas to help me solve my problem?