How do I control a C programme from perl?

2010-05-12 Thread robert Key

Hi,
 I want to capture UNBUFFERED output from a C programme and then 
control it depending on its output. Problem is nothing seems to 
unbufferd the output from the C programme. Only when the child has 
finished executing do I get all the output which is too late.


The C programe is just like hello world in a loop of 20.

Here is the code:
#!/usr/bin/perl -w
use IO::Handle;
sub killGroup {
# kill process group except self
local $SIG{HUP} = 'IGNORE';
kill (HUP, -$$);
print "killed child exit status $?\n";
}

if ($pid = open(READER, "-|")) {
# parent process
print "parent: starting\n";
while () {
print "parent: $_";
killGroup if /3/;
}
close (READER);
} else {
# child process
die "cannot fork: $!" unless defined $pid;
$SIG{HUP} = sub {die "child: killed by parent\n"};

# try and un buffer output
STDOUT->autoflush(1);
print STDOUT "child: starting\n";

# start the C programme which has printf in a loop
exec("/home/robert/try8 2>&1");
exit 0;
}
I have tried all the recipes int the Perl cook book but none work.
Thanks,
   Rob Key

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How do I control a C programme from perl?

2010-05-13 Thread robert Key

On 13/05/2010 14:16, Bob McConnell wrote:

From: robert Key


   I want to capture UNBUFFERED output from a C programme and then
control it depending on its output. Problem is nothing seems to
unbufferd the output from the C programme. Only when the child has
finished executing do I get all the output which is too late.

The C programe is just like hello world in a loop of 20.





I have tried all the recipes int the Perl cook book but none work.


The buffering is probably being done in the C runtime, so it isn't
sending any of the data until it closes. You need to check that code for
a way to disable output buffering.

Bob McConnell


The C programme looks like this (very simple)
#include 
int main()
{
int i;
char s[20] = "hello from c";
for (i = 0; i < 15; i++) {
sleep(1);
printf("%s %d\n", s, i);
}
return 0;
}
and yet I only get the output to perl when it is finished and died.
Thanks,
  Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How do I control a C programme from perl?

2010-05-14 Thread robert Key

thanks so much for your help. I will check it out.
I have been progaming for a long time in fortran, PL/M, c, c++,perl but 
there is always more to learn, great!


I should try this under windows instead of linux and see what happens.
Thanks Eitan.

I have also downloaded the Expect modules and check that out.
Cheers,
   Rob Key

On 13/05/2010 22:44, Eitan Adler wrote:

The C programme looks like this (very simple)
#include
int main()
{
int i;
char s[20] = "hello from c";
for (i = 0; i<  15; i++) {
sleep(1);
printf("%s %d\n", s, i);
}
return 0;
}
and yet I only get the output to perl when it is finished and died.
Thanks,


You get multiple levels of output buffering:
1) C does its own output buffering. Take a look at man setvbuf(1)
2) The operating system buffer which is entirely different than the C buffer
You need to disable both of them to get fully unbuffered output



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How do I control a C programme from perl?

2010-05-14 Thread robert Key

Hi I have downloaded the modules and will give use them.
Thanks for your help,
  Rob Key

On 13/05/2010 14:13, Robert Wohlfarth wrote:

On Thu, May 13, 2010 at 1:21 AM, robert Keywrote:


Hi,
  I want to capture UNBUFFERED output from a C programme and then control it
depending on its output. Problem is nothing seems to unbufferd the output
from the C programme. Only when the child has finished executing do I get
all the output which is too late.



See if the Expector
Expect::Simplemodules
will work. They let Perl control a child process using standard
input and output.




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How do I control a C programme from perl?

2010-05-14 Thread robert Key

Hi Eitan, I have just used fflush and it works.
The real problem is that C programme was just a test programme. I am 
using someone else's programme and I only have the binary! This makes 
life complicated. The only way will be to try and unbuffer output and 
then launch the c binary so that it inherits the unbuffereing that I 
have set.

Thanks very much.
Cheers,
  Rob Key

On 14/05/2010 01:36, wizetux wrote:

On May 13, 1:44 pm, li...@eitanadler.com (Eitan Adler) wrote:

The C programme looks like this (very simple)
#include
int main()
{
int i;
char s[20] = "hello from c";
for (i = 0; i<  15; i++) {
sleep(1);
printf("%s %d\n", s, i);
}
return 0;
}
and yet I only get the output to perl when it is finished and died.
Thanks,


You get multiple levels of output buffering:
1) C does its own output buffering. Take a look at man setvbuf(1)
2) The operating system buffer which is entirely different than the C buffer
You need to disable both of them to get fully unbuffered output


You could also call fflush(stdout); after the printf function.
http://www.thinkage.ca/english/gcos/expl/nsc/lib/fflush.html




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How do I control a C programme from perl?

2010-05-15 Thread robert Key

On 14/05/2010 22:26, C.DeRykus wrote:

On May 14, 8:15 am, robert...@telkomsa.net (robert Key) wrote:

Hi Eitan, I have just used fflush and it works.
The real problem is that C programme was just a test programme. I am
using someone else's programme and I only have the binary! This makes
life complicated. The only way will be to try and unbuffer output and
then launch the c binary so that it inherits the unbuffereing that I
have set.


AFAICT, there's no way to do that. The launched C program won't
inherit any buffering setting. The problem is  easily  solved if
you've
got the C code and can insert a single call to 'setvbuf'. Or simply
inline the code via Perl's Inline::C.

Otherwise, you'll have to use the suggested 'Expect' module so the
C program thinks it's talking to a terminal rather than through a
pipe.

--
Charles DeRykus

Thanks Charles, yup I I actually didn't think it was possible but I have 
installed the Expect and Expect-simple modules. Will do a bit of reading 
there. The C programme I am actually trying to control is jigdo-file. I 
get some errors on file server on night time scheduled downloads, so I 
want to contol them as much as possible, making sure the Debian 
(testing) images I download are all from the same week, checksum errors 
etc. Thanks a lot,

Cheers,
   Rob Key


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




What is the best way to parse a GPX (XML) file

2010-06-16 Thread robert Key
Hi I would like to parse  GPX file (XML) and insert certain data items 
into a MySQL database. There are mulitude of XML modules which can be used.


These modules all build a hash from the data. Is this done because it is 
easy to find the the data simply using the key?


I don't mind writing my own parser although this seems to be reinventing 
the wheel but some like XML::Parser are very general purpose and require 
a huge learning curve.


Any ideas or suggestions would be helpful.
Thanks,
  Robert Key

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: What is the best way to parse a GPX (XML) file

2010-06-16 Thread robert Key

On 16/06/2010 14:29, Owen wrote:



Hi I would like to parse  GPX file (XML) and insert certain data items
into a MySQL database. There are mulitude of XML modules which can be
used.

These modules all build a hash from the data. Is this done because it
is
easy to find the the data simply using the key?

I don't mind writing my own parser although this seems to be
reinventing
the wheel but some like XML::Parser are very general purpose and
require
a huge learning curve.

Any ideas or suggestions would be helpful.
Thanks,
Robert Key





Have a look at http://search.cpan.org/~andya/Geo-Gpx-0.26/lib/Geo/Gpx.pm

Thanks very much I have downloaded Gpx.pm and will check it out.
Rob Key


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/