Click to See Complete Forum and Search --> : C++ vs. Perl
alcdotcom
03-05-2003, 11:53 AM
I'm currently taking a class in C++ (which is of course a Windows based class) and concurrently compiling and testing my programs on my Linux machine. So far, so good. One of my main goals is to convert some of my old bash scripts to C++, but I was wondering if it would be more useful to learn Perl also. Does anyone have any insight on the possible benefits of learning C++ AND Perl? Or can C++ do everything (stream editing and text manipulation, etc.) that Perl can? Thanks!
Anthony
Haunted
03-05-2003, 12:25 PM
I think that everything you can do with Perl you can do with C/C++, but the thing is - Perl makes text manipulation ALOT easeyer! Espeshially if you are familular with Regular Expressions.
Which looks more strait forward? -
C++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
or Perl
#!/usr/bin/perl
print "Hello World\n";
gwartheg
03-05-2003, 12:46 PM
Hi Anthony,
I've taught myself Perl and have been using it daily in my work for about a year and a half. I'm also taking a C++ class right now. Perl can do a lot that C++ can't; the regex that Haunted mentioned, for instance. The Perl interpreter is much more forgiving than the C++ compiler. However, C and C++ are really more powerful and faster than Perl. The Perl compiler and interpreter is written in C, after all. And while Perl is "sort of" object-oriented, C++ is really the language of choice for software design. So anyway, I'm learning both, because knowing both can't hurt. Perl for text manipulation and CGI, and C++ for a more complete understanding of how programming works.
gwartheg
alcdotcom
03-05-2003, 12:48 PM
Haunted,
I see your point. And I am really familiar with regular expressions. Sed and awk are also my friends. However, my scripts are just a tad more complicated than sending "hello world" to standard out. ;) So, I see your point, but can you think of an example where using regular expressions in Perl will save me a significant amount of time and space over using C++?
For example...in one of my bash scripts I use sed scripts in combination with while loops, case statements, and if statements as filters to trap user input errors. This script is also an installer for its main program that creates an installs a custom man page, creates custom wrapper scripts for certain unix programs, and innumerable other things based on a few user inputs. I know these are very abstract ideas, but I'm looking for some examples that would be more applicable. Thanks.
alcdotcom
03-05-2003, 12:52 PM
gwartheg,
Can you use C++ to call Perl or other shell scripts? Now that would be the best of both worlds: full featured object oriented design...quick and powerful text manipluation and ease of use.
Have you heard of people mixing languages like that?
Anthony
gwartheg
03-05-2003, 02:11 PM
Originally posted by alcdotcom
gwartheg,
Can you use C++ to call Perl or other shell scripts? Now that would be the best of both worlds: full featured object oriented design...quick and powerful text manipluation and ease of use.
Have you heard of people mixing languages like that?
Anthony
Hrmm.. I don't know enough about C++ yet to know if it can do what you're asking, but you can make system calls within Perl:my @lines = `foo.sh file`;
system("foo.sh"); # if you don't want the output
exec "sort $filename"; # hands control over to sort and does not return Perl has been referred to as "duct tape" by more than one O'Reilly book :)
gwartheg
chrism01
03-05-2003, 02:42 PM
Hers' a quote from The Perl Cookbook; Chap 6 Pattern Matching:
"Pattern matching isn't like direct string comparison, even at its simplest. It's more like string searching with mutant wildcards on steroids."
Perl has (probably) the most extensive set of regex operators/options of any language.
It also runs about 80% of C speed for less effort in writing and debugging.
C++ is good if you really need proper OO programming.
You can call Perl from C, so i imagine you can from C++.
Try www.perl.org for details
alcdotcom
03-05-2003, 02:56 PM
Brilliant...I'll check it out. Thanks.
Anthony
awesomejt
03-10-2003, 11:36 AM
My personal experience with C, C++, Perl and other shell languages. All have their place and strengths and weaknesses. You need to figure out what you want to do and then use the language that best addresses your needs. Think of languages as "tools" -- that's why I try to keep up on C, Java, Perl, Bash, etc. They are all valuable tools when used correctly.
C == fast, procedural
C++ == fast, object oriented, tool used for serious applications
Perl == good for text (hence the name), outputting text, and really nice in CGI applications.
Bash == BAT done right. Nice for scripting OS commands, basic install scripts, and calling other programs.
Just my 2.3 cents worth.
bwkaz
03-10-2003, 01:49 PM
There are regex libs for C and C++, too, FYI.
One example is Qt's QRegExp (http://doc.trolltech.com/3.1/qregexp.html) class, although that's just a part of a gigantic library; you wouldn't want to include all of Qt just for access to its QRegExp class.
That's the thing with C/C++ -- there may not be support integrated into the language for quite a few things, but you can always find (or write) a library that'll do it for you.
alcdotcom
03-10-2003, 05:04 PM
Why are you "campaigning against using namespace std?" I just started with C++ in January and our instructor always has us put that line in there. But this is a really basic class. Anyway, I'm just curious. Thanks.
Anthony
bwkaz
03-10-2003, 07:39 PM
Because it defeats the entire purpose of even having a std namespace! The symbols in the standard C++ libraries are put into a separate namespace so that they don't accidentally conflict with anything the programmer creates. Doing this also cuts down on namespace pollution.
You should be putting std:: in front of all your standard C++ symbols, but if you don't want to do that, using std::cout; using std::endl; (etc., etc.) are "acceptable" (to me ;)).
Importing the entire std namespace pollutes the default namespace, and creates a possibility of symbol conflict.
fishhead
03-10-2003, 09:22 PM
Originally posted by alcdotcom
gwartheg,
Can you use C++ to call Perl or other shell scripts? Now that would be the best of both worlds: full featured object oriented design...quick and powerful text manipluation and ease of use.
Have you heard of people mixing languages like that?
Anthony
I do this quite often. I use PERL for text processing stuff, shell for system stuff, and C / C++ for most other stuff. Just have them
a) call one another
or
b) pass information between one another via pipes or text files