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?

In reply to A Tricky Problem with Context by tlhf

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.