in reply to Combining two lists into a hash

Does perl-ish mean unreadable, less control over the output, or what? Maybe I am confused or out of my perl-brain, but I do not understand the desire to take perfectly working code that complies with the appearance/semantics of most other languages (C, C++, Java, Python etc) so that it complies with other peoples idea of what 'perl' should look like.

Conformity for the sake of non-conformity?

I think that your post is pointless.

Replies are listed 'Best First'.
Perl idioms (was Re^2: Combining two lists into a hash)
by Tanktalus (Canon) on May 04, 2005 at 02:59 UTC

    Perlish means taking advantage of perl control structures, data types, and abstractions. It means familiar to someone whose sole language is perl. That some of these perlish constructs look identical to constructs from other languages is not a problem. In fact, I'm sure that most of these constructs were stolen from those other languages anyway.

    Abstractions such as using:

    @hash{@a} = @b;
    instead of:
    for (my $i = 0; $i <= $#a; ++$i) { $hash{$a[$i]} = $b[$i]; }
    are not only much clearer and cleaner, they probably run faster, too. Perl can pre-allocate a full hash of the right size, meaning only a single allocation. It can really get in there, optimising speed and space, which the C-style code cannot do. In C-land, one would have to do all those optimisations by hand: you'd need an array object which kept track of the length (because counting the array to find the length takes too long for large arrays); you'd need a hash object which could be told to pre-allocate a certain amount of space; you'd need to also remember each time you go populate the hash to pre-allocate the amount of space you need to keep re-allocations to a minimum.

    In perl, all that extra overhead is handled for you. If you follow the perl idiom.

      @hash{@a} = @b;
      is exactly the sort of thing I was looking for! Several other great examples have been posted, which I also appreciate and which are enlightening in their own ways. This particular approach, though, is what I meant by "Perl-ish" and "esthetic": it's a higher-level way of implementing the function that is both less work for the programmer, and more clear for potential readers--the sort of thing that Perl can excel at, compared to a language like C of Java.

      Thanks, everyone!
Re^2: Combining two lists into a hash
by johnnywang (Priest) on May 04, 2005 at 01:58 UTC
    I think it's a good idea to learn/use the idioms of a language, especially if it provides simplicity and doesn't obfuscate code. I'd prefer to speak a language natively, rather than perl with a C accent. That being said, however, perl does allow more freedom to make itself write-only, each of us will have to appeal to our senses.