LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Help with perl code port to C++ (http://www.linuxquestions.org/questions/programming-9/help-with-perl-code-port-to-c-821364/)

cletcher 07-21-2010 08:33 PM

Help with perl code port to C++
 
Hi there. I've got a (very) simple perl xinetd server that I'd like to port to C++. I've beat my head against the wall trying to learn some basic C++ and implement something similar using system() or popen() without success. Perhaps some kind soul could help me out a bit, or at least let me know what basic components I'd need to make this work.

Thanks in advance.

Code:

#!/usr/bin/perl
use strict;
# Turn off output buffering.
$| = 1;

# Indicate successful connection.
print "Connected to $ENV{REMOTE_HOST} stream server:\n";
# Display initial prompt.
print "[stream]\$ ";
while ( <STDIN>) {
    next unless /\S/; # blank line
    #quit/exit
    if (/quit|exit/i) { last;}
    #print something
    elsif (/date|time/i) { printf "%s\n", scalar localtime;}
    #execute a command on the server
    elsif (/services/i ) { print `cat /etc/services 2>&1`;}
    #else print all the commands
    else { print "Commands: quit date services\n";}
} continue {
    # Print new command prompt.
    print "[server]\$ ";
}


Sergei Steshenko 07-21-2010 08:40 PM

Quote:

Originally Posted by cletcher (Post 4041260)
Hi there. I've got a (very) simple perl xinetd server that I'd like to port to C++. I've beat my head against the wall trying to learn some basic C++ and implement something similar using system() or popen() without success. Perhaps some kind soul could help me out a bit, or at least let me know what basic components I'd need to make this work.

Thanks in advance.

Code:

#!/usr/bin/perl
use strict;
# Turn off output buffering.
$| = 1;

# Indicate successful connection.
print "Connected to $ENV{REMOTE_HOST} stream server:\n";
# Display initial prompt.
print "[stream]\$ ";
while ( <STDIN>) {
    next unless /\S/; # blank line
    #quit/exit
    if (/quit|exit/i) { last;}
    #print something
    elsif (/date|time/i) { printf "%s\n", scalar localtime;}
    #execute a command on the server
    elsif (/services/i ) { print `cat /etc/services 2>&1`;}
    #else print all the commands
    else { print "Commands: quit date services\n";}
} continue {
    # Print new command prompt.
    print "[server]\$ ";
}


Which lines of the Perl script suggest you need to use 'system' or 'popen' in C++ ?

cletcher 07-21-2010 08:56 PM

This one specifically. In perl the `` allow you to execute a command and capture the output.

Code:

elsif (/services/i ) { print `cat /etc/services 2>&1`;}

Sergei Steshenko 07-21-2010 09:04 PM

Quote:

Originally Posted by cletcher (Post 4041284)
This one specifically. In perl the `` allow you to execute a command and capture the output.

Code:

elsif (/services/i ) { print `cat /etc/services 2>&1`;}

Oh :). That's redundant. I.e. had the original coder written

Code:

my $x = `cat /etc/services 2>&1`;
, one would have understood that he/she wants to capture STDOUT of the command into $x variable.

But in this case the stdout is directly passed to 'print', so one could simply not capture it and call 'system' directly - look at the following snippet:


Code:

sergei@amdam2:~/junk> cat -n junk.pl
    1  #!/usr/bin/perl -w
    2
    3  use strict;
    4  use warnings;
    5
    6  print "bacticks begin\n";
    7  print `echo FOO`;
    8  print "bacticks end\n\n";
    9
    10  print "system begin\n";
    11  system("echo FOO");
    12  print "system end\n\n";
sergei@amdam2:~/junk> ./junk.pl
bacticks begin
FOO
bacticks end

system begin
FOO
system end

sergei@amdam2:~/junk>

.

cletcher 07-21-2010 09:10 PM

I'm pretty well versed in perl programming. What I'm wondering is how would one go about writing a program that functions similarly in C++.

Sergei Steshenko 07-21-2010 09:13 PM

Quote:

Originally Posted by cletcher (Post 4041296)
I'm pretty well versed in perl programming. What I'm wondering is how would one go about writing a program that functions similarly in C++.

The line we've just discussed can be modified to use 'system' and as such can be directly translated into C++.

So, what else remains ?

Which is the first line/statement you have problems translating into C++ ?

cletcher 07-21-2010 09:16 PM

Can you capture the output of system() in a variable or something in C++?

Sergei Steshenko 07-21-2010 09:20 PM

Quote:

Originally Posted by cletcher (Post 4041303)
Can you capture the output of system() in a variable or something in C++?

If you want to capture STDOUT, you will need 'popen' and the like. But, as I said, it's not the case in this script, i.e. captured STDOUT is immediately printed into STDOUT, so what's the point ?

cletcher 07-21-2010 09:25 PM

You are correct, thanks for the clarification. For my interest, could you describe how popen could be used in to store multiple lines of a shell command output?

Sergei Steshenko 07-21-2010 09:29 PM

Quote:

Originally Posted by cletcher (Post 4041313)
You are correct, thanks for the clarification. For my interest, could you describe how popen could be used in to store multiple lines of a shell command output?

man 3 popen
.

cletcher 07-21-2010 09:32 PM

Thanks, I already looked at that. It's pretty cryptic at my current skill level. I googled around to find some examples and played with it a bit myself. I guess I'm still missing some understanding of the basic parts of C++. More reading might be in order....

Sergei Steshenko 07-21-2010 09:40 PM

Quote:

Originally Posted by cletcher (Post 4041322)
Thanks, I already looked at that. It's pretty cryptic at my current skill level. I googled around to find some examples and played with it a bit myself. I guess I'm still missing some understanding of the basic parts of C++. More reading might be in order....

First of all, the manpage is about "C" which in this case can be used in C++ as a subset. My C++ is shallow; there may be equivalent function is C++, but ultimately they call the same standard "C" library functions.

I suggest to also read

man 3 fread
man 3 fwrite

- because after a pipe is open, dealing with it is the same (more rather than less) as with a regular file opened through 'fread' or 'fwrite'. That's the whole point of UNIXish "everything is a file" concept.

cletcher 07-21-2010 09:44 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4041327)
First of all, the manpage is about "C" which in this case be used in C++ as a subset. My C++ is shallow; there may be equivalent function is C++, but ultimately they call the same standard "C" library functions.

That's probably a large portion of my problem. I'm glad you pointed that out.

Sergei Steshenko 07-21-2010 10:31 PM

Quote:

Originally Posted by cletcher (Post 4041332)
That's probably a large portion of my problem. I'm glad you pointed that out.

In the vast majority of cases C++ is a superset of "C", so you simply can't have full C++ mastership not understanding plain old "C". C++ was conceived with as much as possible "C" compatibility in mind.

My point is that I really don't understand what the problem is with 'popen' being traditional "C" :).


All times are GMT -5. The time now is 04:24 AM.