PBP: 060 Slicing

Everyone’s favorite set of Perl guideliens tells is not to forget about the awesome power of array slices!  That’s smart, too.

The book then goes on to give shiny examples, and warn of a few pitfalls of using array slices.  They’re good and clear.

Part of the book’s age is showing in that it does not mention the hash slice, added less than a year ago in Perl 5,20.  They’re another way to extract data from a hash, given a specific set of keys, like an array slice.  Neat, but I haven’t bumped into a use in the wild yet.

I do find the reason the book gave for using array slices to be true; readability and clarity.  If you’re extracting several array items in a row, that’s a slice, and it might be more clearly written that way.

On the other hand, a lot of Perl programmers don’t know slices exist, and will tell you that leading an array use with @ will not work.  This means that using them makes the code readable and maintainable by only very advanced engineers.  I find this at odds with other advice in the book, which caters to people stuck on hard terminals and awk programmers and things.  I prefer to lean this way; encourage deeper understanding of the language, and use the features available.  It’s a teachable moment for any engineer who isn’t familiar with slices.  Seize it and encourage them to grow!

This means, too, that we can let awk programmers keep up with the times, and encourage everyone to get bitmapped displays with real fonts.  I’m sure I’ll have more to rant about this when I get to Chapter 12.

3 Responses to “PBP: 060 Slicing”

  1. Jay Allen says:

    Ugh, I just left a comment but the page went white after I submitted and now it’s gone. Let me know if I posted it to the ether and I’ll rewrite it.

  2. Jay Allen says:

    Since that comment appeared immediately, I’ll assume that it’s gone. Here goes again…

    PBP should have mentioned hash slices because they’ve been around forever, long before its publication date. By that I mean this syntax which allows you to extract multiple values at a time from a hash:

    my %letters = ( a => 1, b => 2, c => 3, d => 4 );
    my @odd = @letters{qw( a c )); # @odd == qw( 1 3 )
    my @even = @letters{qw( b d )); # @even == qw( 2 4 )

    Perl 5.20 simply added a new variant to that syntax which allows you to extract the keys and the values together. From perldata:

    %h = (blonk => 2, foo => 3, squink => 5, bar => 8);
    %subset = %h{‘foo’, ‘bar’}; # key/value hash slice
    # %subset is now (foo => 3, bar => 8)

    I’m not sure if this is what you meant originally, but if someone is discovering slices for the first time by reading your post, I figure this should be explicit. :)

  3. The new variant of Perl 5.20 is actually extremely useful, and gives parity with the array slice behaviour. Previously, all slices whether on an array or a hash resulted in an array. But the new hash variant is a true hash slice, that returns a hash; it is analogous to what you get in SQL when you do a projection; the output columns still know what their names are as in the originals, rather than this being lost.

Leave a Reply