Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl Equivelent of C++ program

Reply
Thread Tools

Perl Equivelent of C++ program

 
 
John Wilkinson
Guest
Posts: n/a
 
      03-22-2007
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
 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      03-22-2007
John Wilkinson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
 
 
 
John Wilkinson
Guest
Posts: n/a
 
      03-22-2007
On Thu, 22 Mar 2007 15:43:06 +0000, Ian Wilson wrote:

> John Wilkinson wrote:
>> 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
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      03-22-2007
John Wilkinson <(E-Mail Removed)> wrote:
> On Thu, 22 Mar 2007 15:43:06 +0000, Ian Wilson wrote:
> > John Wilkinson wrote:
> >> 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
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-22-2007
John Wilkinson <(E-Mail Removed)> wrote:


> 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.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      03-22-2007
Tad McClellan <(E-Mail Removed)> wrote:
> John Wilkinson <(E-Mail Removed)> wrote:
> > 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.


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
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      03-23-2007
Jens Thoms Toerring <(E-Mail Removed)> wrote in comp.lang.perl.misc:

[...]

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl equivelent of "links -dump" Graham Drabble Perl Misc 2 10-18-2009 03:43 PM
Re: Does '!=' equivelent to 'is not' Leo Jay Python 16 06-19-2008 06:34 AM
Getting ApplicationPath (or equivelent) in Application_Start handler bryan@newsgroups.nospam ASP .Net 8 10-20-2005 01:03 AM
Digital 17mm Is Not Equivelent 27mm on 35mm Film A Digital Photography 28 12-22-2004 04:14 PM
Is there anything equivelent to Fujis S602,,,,,,,,,,,,,, Sharonius Digital Photography 2 12-29-2003 07:53 PM



Advertisments