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.
  • I have two opposing examples of languages that are more likely to need comments than others: regexps and T-SQL (Sybase/SQL Server). One is extremely terse, one is extremely verbose.

    Regexps are usually very compact and opaque. Your comment about whitespace is spot on, so /x should obviously be encouraged. But even with a sanely laid out regexp, the pattern of a regexp usually needs comments to indicate exactly what (\w+-\d) means in the context of a program. The regexp syntax itself doesn't allow us to do t
    • When it comes to Perl, I find that using implicit $_ often obscures the code. Not using an explicit variable means a missed opportunity to self-document the code by naming the variable properly.

      I think the exact opposite. $_ has a meaning in Perl: “the current ‘thing’ being operated on”. That's a well-defined convention, and it has the advantage of being the same in every program.

      In other languages when people want a temporary variable they often use i or n or s, but they aren't more readable than $_.

      If you've got an @server array of objects, and perhaps a $server_num variable too, and you want to iterate over each server, you could call the iterator variable $server, but then you have several variables with very similar names. Far better to use the form of foreach that doesn't explicitly name an iterator variable to avoid having to synthesize an artificial name, and use the standard name that Perl already provides.

      • Using $i as a loop variable doesn't increase readability unless it is to communicate that "it's just a loop variable" and that's seldom the case since the Perl foreach is so much more useful than the C-style for loop.

        If the code block is very brief, like in

        dostuff($_) for(@servers);

        then I agree that it isn't necessary to alias it to a new name. But if the code block is longer than a few lines, it's indeed very useful to have an actual name connected to the entity you're dealing with.

        Consider the followi