jkollar has asked for the wisdom of the Perl Monks concerning the following question:
I would like to create an API routine for accessing a proprietary database that has a SQL-like language syntax. For example, I would like to do something like the following:
my $value = ''; DBC ( '$value=DBrecord1,subrecord,field', '$x=DBrecord1,subrecord,field2', 'DBrecord1,subrecord,field3=55', 'DBrecord2,subrecord,field=$value' ); print "Copied $value from DBrecord1 to DBrecord2.\n"; print "Read $x from DBrecord1.\n"; print "Wrote 55 into DBrecord1.\n";
The essence of what I want to do is to have the DBC subroutine parse my specialty language into more primitive API routines that access the database. The issue I am having is in how to relate '$value' (notice that I have deliberately used single quotes to prevent variable interpolation into the string) to the corresponding Perl variable that is in the scope of the calling routine.
I have tried a less elegant syntax that uses references, but ultimately still encounter the problem of how to (I think) access the symbol table that is seen in the caller's scope. For example, I have tried the following:
DBC ( 'DBrecord1,subrecord,field' => \$value, 'DBrecord1,subrecord,field2' => \$x, 55 => 'DBrecord1,subrecord,field3', $value => 'DBrecord2,subrecord,field' );
This is a less intuitive syntax, and it does not quite satisfy my needs. If each command were to be submitted to the database and completed before the next command is started then this would work. However, for performance reasons I need to be able to submit the commands to the database at once (i.e. this is the reason that I am not just calling DBC four times with one command each time). What I really want to do in the example shown is to have DBC recognize that the same variable appears in both the 1st and 4th commands, and then to split the commands into two database submissions -- the first three commands in the first, and the 4th command in the second.
Given that I am willing to perform whatever complex parsing is required to make the original syntax work, is there a way that I can take a string such as '$value' and from it obtain a reference to the same-named variable in the callers variable scope?
An alternative way of doing this, such as the second syntax I described, would also be helpful. Or perhaps there is a way to use an eval or code-block trick (i.e. a "{}" block instead of the parameter list of a subroutine).
Any insights would be greatly appreciated.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Specialty database API.
by rjt (Curate) on Mar 23, 2013 at 23:33 UTC | |
|
Re: Specialty database API.
by Anonymous Monk on Mar 23, 2013 at 23:12 UTC |