If you want to run it interactively, it usually means sending values to it, and getting values back. The general way to do it is thru IPC::Open3. Search for examples, read perldoc perlipc, or look at this very basic code
#!/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] |
I would use cron for this, but here is a non-robust solution.
Update: For the reasons outlined by Fletch below, don't do this...
#!/usr/bin/perl
use strict;
use warnings;
while(1){
`get_value.pl`;
sleep 120;
}
| [reply] [d/l] |
| [reply] |
I said it was non-robust. Perhaps I should have described it as "icky" instead?
| [reply] |
Under an Unix installations (e.g. Linux) you could use a cron job for this and tell it to start the script every two minutes. Just google after 'cron job'.
To code it in perl, e.g. for Windows without cron daemon, you could write something like this:
while ( 1 ) {
system("perl get_value.pl");
sleep(120);
}
This would delay for two minutes after the get_values script ended. You might want to add an possibility to stop this loop without killing the perl process.
Update:
This might look like a repost of an above post, but I got interrupted for an half hour after I wrote this post but before I could sent it. Afterwards I was just sending it without checking if anyone else gave the same answer in the meantime. | [reply] [d/l] |
It's OK, you used system. My backticks were met with revulsion. :-)
| [reply] |
I'll see your revulsion and raise you a silent bug. The above used system without separating the arguments, or checking return value. The perldoc shows better practice. If you're writing immutable code on your own box, you can obviously shove it all into one string but it's not a habit to cultivate.
my @args = qw( perl get_value.pl );
system(@args) == 0
or die "system @args failed: $?"
| [reply] [d/l] [select] |
There is a Windows version of cron
here (Disclaimer: Never used it).
--
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
| [reply] |