Python/C++ vs. Perl/C++


Here's a little exercise that should demonstrate that Python is a more natural scripting language option for C/C++ programmers (and human beings in general) than Perl. This is but one of many possible examples.

The task: Print the name of the currently running process to stdout followed by a newline.

Comment on this topic

cout << argv[0] << '\n';

Easy enough.

print "$0\n";

OK, pretty strange looking if you aren't used to Perl. Very concise. But quite cryptic. $0 is one of Perl's many special variables and it happens to contain the name of the running process. Now, Perl neophytes might be tempted to use Perl's built in array ARGV. This would seem logical for those coming from the C/C++ world. If you write print "$ARGV[0]\n"; (don't forget the $), though, you won't get what you expected. Why? Well, Perl has redefined the meaning of the array traditionally named "argv". In C and C++ (and other languages, as you'll soon learn), argv contains the arguments to the process, including, as argv[0], the name of the process. In Perl, ARGV only contains the arguments to the process, not the process name.

print sys.argv[0]

Python has chosen to not redefine the argv array, so the process name resides in the expected place. The only possible confusion in the Python version is that argv lives in the "sys" module, so you need to qualify the name with sys..