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.
  • Until more of the world switches to taking a functional perspective, I suspect that procedural code is going to be more understandable (and maintainable) by the general population of developers.

    Paradigm shifts often take time.

    • Here! Here!

      The problem with functional code is that most fresh meat, er, new hires/newbies won't grok what's going on in the code. By using procedural idioms over the functional ones, your code is more likely to be understood by others (and maybe even you) in the future. Using language neutral idioms in public Perl code goes a very long way to squashing Perl's "unmaintainable, spaghetti code" reputation.

      Does that mean eschewing map and grep all together? I don't think so. It is better to limit the use of these operators to idiomatic usage. For instance, grep is a great way to filter array values.

      my @private_keys = grep { /^_/ } keys %hash;

      map ought not to be a blank replacement for foreach, but rather used to create hashes that have easily computed values.

      my %hashed_names = map{ $_ => md5_hex($_) }@names;

      If you need to do more complicated things inside a map or grep please consider using a traditional for or while loop. At some point in the future, I think you'll be glad you did.

      In the interest of full disclosure, I too went through a phaze where I was all about map and grep. I now shudder when I look at that code.

      That said, if you're writing your code just for you, you can get down with your bad functional self.

      • My favourite current map/grep idiom is:

        opendir(DIR, $somedir);
        my @files = grep { -f } map { "$somedir/$_" } readdir(DIR);

        Nice and succinct, and I think easy to understand (though I should probably be using File::Spec for portability, but *shrug*). I also partly wish you could do filters like this, sort of like SAX pipelines, so I could do something like:

        my (@files, @dirs) = grep2 { -f, -d } map { "$somedir/$_" } readdir(DIR);

        I'm sure pdcawley will now pipe in how Perl 6 will be able

        • Here's one I've found handy on occasion:

                  @stuff = sort keys %{{ map {$_ => undef} @stuff }};

          gets a sorted list of uniq entries in @stuff.

          I always use undef instead of 1 for insignificant hash values because it just *seems* like it should be more efficient. I've never benchmarked any stress-tests though.

      • By using procedural idioms over the functional ones, your code is more likely to be understood by others (and maybe even you) in the future.

        Any style can be abused to produce good or bad Perl code. Erring on the side of "procedural Perl" for the benefit of newbies doesn't mean it will naturally be easier to understand. I've seen grotty Perl code that's using a classic FORTRAN style, so I'd say that it's more important to write clearly than to write in a specific style. I've also seen code that's bee