Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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 or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2021-10-24 23:05 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (89 votes). Check out past polls.