This is how you might do it. At least it's a start. When you read the results from your commands, do whatever you want depending on the returned text. I think what you really need to do here, is substitute /bin/bash with your script, 'myprocessor.pl'. Then,
have myprocessor.pl accept commands on stdin, run appropriate subs, and return answers.
#!/usr/bin/perl
use warnings;
use strict;
use IPC::Open3;
use IO::Select;
my $pid = open3(\*WRITE, \*READ,\*ERROR,"/bin/bash");
my $sel = new IO::Select();
$sel->add(\*READ);
$sel->add(\*ERROR);
my($error,$answer)=('','');
while(1){
print "Enter command\n";
chomp(my $query = <STDIN>);
#send query to bash
print WRITE "$query\n";
foreach my $h ($sel->can_read)
{
my $buf = '';
if ($h eq \*ERROR)
{
sysread(ERROR,$buf,4096);
if($buf){print "ERROR-> $buf\n"}
}
else
{
sysread(READ,$buf,4096);
if($buf){print "$query = $buf\n"}
}
}
}
waitpid($pid, 1);
# It is important to waitpid on your child process,
# otherwise zombies could be created.
| [reply] [d/l] |
The easy way to fake a command interpreter is to reuse an existing command interpreter. The one command interpreter you have at hand is Perl, and Perl has the eval statement, which gives you the whole Perl interpreter to use.
So one approach would be to take your command language and convert it into a Perl program. If you give your command language enough thought beforehand, this can be done by something as simple as Filter::Simple, just like Querylet does, or maybe Filter::QuasiQuote, if you don't mind the wrapper of your program looking like Perl.
The following is a simple interpreter for arithmetic expressions, as long as they are on one line:
use strict;
while (<>) {
my $res = eval $_;
if (my $err = $@) {
warn "Error: $@";
} else {
print "'$_' gives $res\n"
};
};
| [reply] [d/l] |
Well, for a start you could change the node title :-)
Seriously, I don't know where you could start from:
"defining a syntax" can be a big job if you want a language with a minimum of flexibility, or a trivial one if you have only a handful of commands to implement. It really depends on what your program should do, and you have not given much detail.
On the parser side, there are many choices available but I never did these things in Perl, so it's better for somebody else to advise you.
Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."
| [reply] |