Perl Equivelent of C++ program

Discussion in 'Perl Misc' started by John Wilkinson, Mar 22, 2007.

  1. 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
     
    John Wilkinson, Mar 22, 2007
    #1
    1. Advertising

  2. John Wilkinson

    -berlin.de Guest

    John Wilkinson <> 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
     
    -berlin.de, Mar 22, 2007
    #2
    1. Advertising

  3. 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
     
    John Wilkinson, Mar 22, 2007
    #3
  4. John Wilkinson <> 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://toerring.de
     
    Jens Thoms Toerring, Mar 22, 2007
    #4
  5. John Wilkinson <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 22, 2007
    #5
  6. Tad McClellan <> wrote:
    > John Wilkinson <> 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 ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Mar 22, 2007
    #6
  7. John Wilkinson

    -berlin.de Guest

    Jens Thoms Toerring <> 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
     
    -berlin.de, Mar 23, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    4,770
    Steven Cheng[MSFT]
    Oct 20, 2005
  2. Leo Jay
    Replies:
    16
    Views:
    503
    Derek Martin
    Jun 19, 2008
  3. Jules
    Replies:
    40
    Views:
    418
    Jules
    Dec 28, 2005
  4. Christopher M. Lusardi
    Replies:
    3
    Views:
    215
    Alan J. Flavell
    Oct 19, 2003
  5. Graham Drabble

    Perl equivelent of "links -dump"

    Graham Drabble, Oct 18, 2009, in forum: Perl Misc
    Replies:
    2
    Views:
    107
    Graham Drabble
    Oct 18, 2009
Loading...

Share This Page