I cast my incantation in C, diving headlong into the danger:
void version(char *progname, char *rcs_rev) { int major = -1; int minor = -1; char *where = NULL; char buf[5]; char nulls[5] = {'\000', '\000', '\000', '\000', '\000'}; int bufptr = 0; if(strchr(rcs_rev, ':') == NULL) { printf("%s, no RCS revision given.\n", progname); return; } where = strchr(rcs_rev, ' '); where++; while(where[0] != ' ') { if(where[0] != ' ' && where[0] != '.') { buf[bufptr++] = where[0]; } else if(where[0] == '.') { if(major == -1) { major = atoi(buf); } else if(minor == -1) { minor = atoi(buf); break; } bufptr = 0; memcpy(buf,nulls,5); } else break; where++; } fprintf(stderr,"%s version %d.%d\n",progname,major,minor); }

Then I realized I had spent an hour writing a simple getopt thing for my small tool (clone of 'from'), and most of time had been sunk on that part that merely prints the version number from RCS.

Then, in 2 minutes, I wrote the same in Perl:

sub version { my ($progname, $rcs_rev) = (@_); if($rcs_rev !~ /:/) { print "$progname, no RCS version given.\n"; return; } $rcs_rev =~ /Revision: (\d+)\.(\d+)/; my ($major, $minor) = ($1, $2); print "$progname version $major.$minor\n"; }

I hope people will learn to understand why I think Perl is such a nice language.

Replies are listed 'Best First'.
RE: What I Did Today To Prove Perl Beats C
by mikfire (Deacon) on May 24, 2000 at 20:07 UTC
    Be wary of over generalization. My counter example for the day is SNMP. If you have never played with it, don't. Suffice to say that part of SNMP involves a great deal of bit twiddling.

    In my shop, we had a set of utilities based on a pure perl SNMP solution. When I was asked to upgrade and fix the utilities, I immediately freed perl from its bondage and made C do the bit-twiddling via the ucd-snmp libraries and the SNMP module.

    The utilities which previously took minutes to run now take mere seconds.

    To every language its baliwick; elegance results when each is used in its strength to solve a problem.

    Mik
    Mik Firestone ( perlus bigotus maximus )

      How does this prove Perl superiority?

      It looks to me like it proves that C is good for something (which we knew... almost every language {except lisp} that is in use is in use for a good reason.)

        Those who don't learn from history are doomed to repeat it. While you bash Lisp, the great Perl hackers are trying hard to imitate it.
        • Lisp was the first language implemented in itself -- the first one to have an eval function at all!
        • Lexical scope and closures (my() and anonymous subs, respectively) were invented in Scheme (25 years ago).
        • Lispers were early adopters of OO. In the early 70's we already had excellent OO extensions such as LOOPS, and today's full-featured Common Lisp specification includes a Common Lisp Object System which is incredibly complete and flexible (not to mention complex!).
        • Where'd you think map() came from? What about grep()? (Of course, it's called filter in scheme...)
        • Hell, even the idea of lhs subroutine calls (new as of Perl 5.6) came up in Lisp - some good 30 years ago, even! But why linger in the past? :) The features which are standard in R5RS-compliant Scheme implementations today (e.g. continuations, promises, hygienic macros, even the trivial but enormously useful let variants for scoping with variables), as well as some of the nicer ones from particular extensions (e.g. logical programming and soft pattern-matching-based typing as in Schelog) just might find their way into Perl 7.0, ten years from now. (Only in an uglier, more C-ish way, of course. :))
        Well, yes. That was my point. Perl does not "beat" C. Perl solves certain problem spaces better than C, but C solves certain problems spaces better than Perl. Mine was a cautionary tale.

        mikfire

RE: What I Did Today To Prove Perl Beats C
by mdillon (Priest) on May 24, 2000 at 20:46 UTC
    i think the fact that Perl itself is written in C is a testament to its utility (as if we all didn't already know C was useful). There is More Than One Way to Do It, and sometimes it involves something other than Perl.
      Ah, but think how much faster perl could've been written in Perl. :-)
RE: What I Did Today To Prove Perl Beats C
by WWWWolf (Initiate) on May 24, 2000 at 22:42 UTC
    Actually the above was written by me, Junkbuster ate the cookie =(

    I do admit that Perl isn't always better than C. C is a nice language, I use it ocassionally. =)

    Anyway, the bad thing about the above was this: Only a few moments ago, I noticed one thing: "Oh yes, 'sscanf()' does exist, too."

    AaRrrGGhHh.

    Ah, the life of Random C User =)

      "C. It's a nice language. I use it occasionally... :-)"
      -- Larry Wall
RE: What I Did Today To Prove Perl Beats C
by Anonymous Monk on May 24, 2000 at 23:50 UTC
    Here's a nickel. Go buy yourself a decent programming language. void version(const char* progName, const char* versionStr) { int major; int minor; int result; result = sscanf(versionStr, "Revision: %i.%i", &major, &minor); if (2 != result) { printf("%s isn't a valid revision string.\n", versionStr); } else { printf("%s: Major: %i, Minor: %i\n", progName, major, minor); } }