Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • As soon as I saw that ~100 line Java program, I immediately wrote a one-line Perl equivalent:

    while () { print unless /^(192\.(9|18|29)\.|\.(?i:(gif|jpg|css)) HTTP)/; }

    Of course, you could match his argument conventions precisely, but why bother? This form is the normal Perl way to do it, and the author's Perl and Java arguments were already different.

    I haven't benchmarked this one-liner, but I bet it's faster than the author's Perl version, and likely faster than the Java code as well. It might be a shade faster to drop the outermost parens, and avoid capturing with (?:...) and avoid an alternation by replacing 9|18|29 with 2?9|18, but these would come at the expense of readability and might not make a noticable performance impact.

    Also, that one line of Perl code took me only about 2 minutes to write (slowed down slightly be double-checking the (?i:...) construct). I'm betting that 100 lines of Java code took quite a bit longer to write and debug, and it's harder to maintain 100 lines of code than one. In real life, I'd probably have used a /i modifier on the end of the regex instead of using the less-readable (?i:...) construct, but I wanted to match exactly what the author was matching to make it a valid comparison.

    As far as I'm concerned, Perl's "home turf" includes being efficient for the programmer to write programs. Sometimes, highly-optimized runtime is important, but optimizing programming time is frequently more valuable. Even so, short Perl programs like this often have hard-to-beat runtimes if written well, so you can often have your cake and eat it too...


    "Simple things should be simple, and complex things should be possible." - Alan Kay

    • You may want to double check the position of your ^

      As someone said somewhere (petdance iirc), when making optimized solutions, test. It's something a lot of people seem to not be doing in this thread (either here or in davorg's journal). If you're going to make it more efficient, you might as well make it produce the same results.

      At work, I produced a shiny new version of a previous routine. I couldn't really benchmark them though: the previous version processed much less data due to a bug in its impleme
        ---ict / Spoon
      • Actually, I wrote it with the ^ inside the parens originally when I copied it onto here, I dropped the outer parens because they were really redundant. Then I decided to put them back in for readability and mention that they could be dropped instead.

        Unfortunately, when I put them back in, I put the opening paren after the ^ (force of habit) when that's not what I meant. My bad!

        I also lost the <> operator inside the while (), while we're being pedantic. :-) (I'm pretty sure this was eaten by the S


        "Simple things should be simple, and complex things should be possible." - Alan Kay