Perl Equivelent of C++ program

J

John Wilkinson

Hi,
I have a simple C++ program example, I would dearly love to swap to perl.
It uses pipes, which I do not understand, and I was wondering if anyone
could convert this bit of code for me:
#include <stdio>
#include <string>
#include <sys/types>
#include <unistd>

int main (int argc, char* argv{}){

int n;
char line[1024];

while((n = read(STDIN_FILENO, line, 1024)) > 0) {
line[n]= '\0';
if (strcmp(line,"firstcommand" == 0) {
strcpy(line, "answer,= 1234 -2345 155 test");
}
else {
strcpy(line, "unknown command");
}
n = strlen(line);
if (write(STDOUT_FILENO, line, n) != n) {
fprintf(stderr,"Server can't write to stdout\n");
}
}
}

This is supposed to be a simple example server, started by another process,
and using pipes to communicate to this server.

Many regards,
John
 
A

anno4000

John Wilkinson said:
Hi,
I have a simple C++ program example, I would dearly love to swap to perl.

It's a C program.
It uses pipes, which I do not understand, and I was wondering if anyone

It doesn't use pipes as such. It reads from standard input and writes
to standard output. If these happen to be pipes that makes no
difference to your program.
could convert this bit of code for me:

[has been done, code snipped]

Anno
 
J

John Wilkinson

John said:
Hi,
I have a simple C++ program example, I would dearly love to swap to perl.
It uses pipes, which I do not understand, and I was wondering if anyone
could convert this bit of code for me:
#include <stdio>
#include <string>
#include <sys/types>
#include <unistd>

int main (int argc, char* argv{}){

int n;
char line[1024];

while((n = read(STDIN_FILENO, line, 1024)) > 0) {
line[n]= '\0';
if (strcmp(line,"firstcommand" == 0) {
strcpy(line, "answer,= 1234 -2345 155 test");
}
else {
strcpy(line, "unknown command");
}
n = strlen(line);
if (write(STDOUT_FILENO, line, n) != n) {
fprintf(stderr,"Server can't write to stdout\n");
}
}
}

This is supposed to be a simple example server, started by another process,
and using pipes to communicate to this server.

Since I'm not a C programmer I suspect it is equivalent to something not
entirely unlike this (untested):

#!perl
use strict;
use warnings;
my $reply = "Oops";
while(<>) {
if (/^firstcommand/) { $reply = "answer,= 1234 -2345 155 test"; }
else { $reply = "unknown command"; }
print $reply or warn "Server can't write to stdout because $!\n";
}

But I think you will be much better off abandoning your C examples and
starting by looking for Perl examples instead.

Thanks for the help here.

I ran your example, the program started, however it gets no output from
this code.
If I start this from the command line, I have to enter a cr, before I get
back the characters entered. If the program that starts this server is not
sending cr, how can I do this in perl?

Thanks,
John
 
J

Jens Thoms Toerring

John Wilkinson said:
John said:
I have a simple C++ program example, I would dearly love to swap to perl.
It uses pipes, which I do not understand, and I was wondering if anyone
could convert this bit of code for me:
#include <stdio>
#include <string>
#include <sys/types>
#include <unistd>

int main (int argc, char* argv{}){

int n;
char line[1024];

while((n = read(STDIN_FILENO, line, 1024)) > 0) {
line[n]= '\0';
if (strcmp(line,"firstcommand" == 0) {
strcpy(line, "answer,= 1234 -2345 155 test");
}
else {
strcpy(line, "unknown command");
}
n = strlen(line);
if (write(STDOUT_FILENO, line, n) != n) {
fprintf(stderr,"Server can't write to stdout\n");
}
}
}

This is supposed to be a simple example server, started by another
process, and using pipes to communicate to this server.

Since I'm not a C programmer I suspect it is equivalent to something not
entirely unlike this (untested):

#!perl
use strict;
use warnings;
my $reply = "Oops";
while(<>) {
if (/^firstcommand/) { $reply = "answer,= 1234 -2345 155 test"; }
else { $reply = "unknown command"; }
print $reply or warn "Server can't write to stdout because $!\n";
}

But I think you will be much better off abandoning your C examples and
starting by looking for Perl examples instead.
Thanks for the help here.
I ran your example, the program started, however it gets no output from
this code.
If I start this from the command line, I have to enter a cr, before I get
back the characters entered. If the program that starts this server is not
sending cr, how can I do this in perl?

Actually, you will have the same problem with your program if you
run it from the command line and enter the text manually - the
terminal only passes the data on to your program when you have
pressed the <RETURN> key. Moreover, while your program might work
most of the time if the input is coming from e.g. pipe, there's a
problem you should be aware of: read() may return less characters
than the whole string "firstcommand" has, so you may accidentally
skip "good" input!

Of course, you can rewrite the Perl script to use sysread() and
syswrite() (the Perl replacements for the C low level read() and
write() functions) and thus a (more or less) faithfully transla-
ting your program (including the bug) would be

#!/usr/bin/perl
use strict;
use warnings;
while ( sysread STDIN, my $buf, 1024 ) {
if ( $buf eq /^firstcommand' ) {
$buf = 'answer,= 1234 -2345 155 test';
} else {
$buf = 'unknown command';
}
syswrite STDOUT, $buf
or warn "Server can't write to stdout\n";
}

But I don't know if that really helps you much. Perhaps you
better specify exactly what you need instead of just giving
us a slightly broken program to start with. Only then the
things where Perl shines can be used. Like it is it's more
like a Babelfish translation...

Regards, Jens
 
T

Tad McClellan

It uses pipes,


No it doesn't.

which I do not understand,


Pipes are ridiculously easy. Understanding them is not hard.

All a pipe does is to connect the STDOUT of the "left" process
to the STDIN of the "right" process.

If you are writing a program to run in the "left" process, then
make your output on STDOUT.

If you are writing a program to run in the "right" process, then
take your program input from STDIN.

That is all that is required to use a pipe to connect the two programs.
 
J

Jens Thoms Toerring

Tad McClellan said:
Pipes are ridiculously easy. Understanding them is not hard.
All a pipe does is to connect the STDOUT of the "left" process
to the STDIN of the "right" process.

If you are writing a program to run in the "left" process, then
make your output on STDOUT.

If you are writing a program to run in the "right" process, then
take your program input from STDIN.

That is all that is required to use a pipe to connect the two programs.

In principle you're right, but if you use low level I/O C functions
like read() and write() (as the OP did) or the Perls equivalent
functions sysread() and syswrite() it starts to become a tad more
complicated. And then some people assume that pipes can temporarily
store infinite amounts of data and wonder why their programs hang;-)

Regards, Jens
 
A

anno4000

[...]
things where Perl shines can be used. Like it is it's more
like a Babelfish translation...

....and just like Babelfish translations, its utility is much overrated
by people who don't know the target language.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top