LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-21-2010, 08:33 PM   #1
cletcher
LQ Newbie
 
Registered: Oct 2005
Posts: 14

Rep: Reputation: 0
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]\$ ";
}
 
Old 07-21-2010, 08:40 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by cletcher View Post
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++ ?
 
Old 07-21-2010, 08:56 PM   #3
cletcher
LQ Newbie
 
Registered: Oct 2005
Posts: 14

Original Poster
Rep: Reputation: 0
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`;}
 
Old 07-21-2010, 09:04 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by cletcher View Post
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>
.
 
Old 07-21-2010, 09:10 PM   #5
cletcher
LQ Newbie
 
Registered: Oct 2005
Posts: 14

Original Poster
Rep: Reputation: 0
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++.
 
Old 07-21-2010, 09:13 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by cletcher View Post
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++ ?

Last edited by Sergei Steshenko; 07-21-2010 at 09:21 PM.
 
Old 07-21-2010, 09:16 PM   #7
cletcher
LQ Newbie
 
Registered: Oct 2005
Posts: 14

Original Poster
Rep: Reputation: 0
Can you capture the output of system() in a variable or something in C++?
 
Old 07-21-2010, 09:20 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by cletcher View Post
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 ?
 
Old 07-21-2010, 09:25 PM   #9
cletcher
LQ Newbie
 
Registered: Oct 2005
Posts: 14

Original Poster
Rep: Reputation: 0
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?
 
Old 07-21-2010, 09:29 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by cletcher View Post
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
.
 
Old 07-21-2010, 09:32 PM   #11
cletcher
LQ Newbie
 
Registered: Oct 2005
Posts: 14

Original Poster
Rep: Reputation: 0
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....
 
Old 07-21-2010, 09:40 PM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by cletcher View Post
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.

Last edited by Sergei Steshenko; 07-21-2010 at 10:28 PM.
 
Old 07-21-2010, 09:44 PM   #13
cletcher
LQ Newbie
 
Registered: Oct 2005
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 07-21-2010, 10:31 PM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by cletcher View Post
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" .
 
  


Reply

Tags
c++, perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in Perl Code : Bad switch statement(Problem in code block)? near ## line # suyog255 Programming 4 02-20-2008 06:35 PM
What do those perl code do? cn_chopsticks Programming 10 06-07-2006 12:03 AM
What would this code do in PERL? jdruin Linux - Software 2 11-22-2004 04:12 AM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 04:22 PM


All times are GMT -5. The time now is 10:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration