Earlier this week, I read a post from someone who failed a job interview because they used a hash slice in some sample code and the interviewer didn’t believe it would work.
That’s not just wrong — it’s a teachable moment. Perl has several kinds of slices, and they’re all powerful tools for writing expressive, concise, idiomatic code. If you’re not familiar with them, you’re missing out on one of Perl’s secret superpowers.
In this post, I’ll walk through all the main types of slices in Perl — from the basics to the modern conveniences added in recent versions — using a consistent, real-world-ish example. Whether you’re new to slices or already slinging %hash{...}
like a pro, I hope you’ll find something useful here.
The Scenario
Let’s imagine you’re writing code to manage employees in a company. You’ve got an array of employee names and a hash of employee details.
1 2 3 4 5 6 7 8 9 |
my @employees = qw(alice bob carol dave eve); my %details = ( alice => 'Engineering', bob => 'Marketing', carol => 'HR', dave => 'Engineering', eve => 'Sales', ); |
We’ll use these throughout to demonstrate each kind of slice.
1. List Slices
List slices are slices from a literal list. They let you pick multiple values from a list in a single operation:
1 2 |
my @subset = (qw(alice bob carol dave eve))[1, 3]; # @subset = ('bob', 'dave') |
You can also destructure directly:
1 2 |
my ($employee1, $employee2) = (qw(alice bob carol))[0, 2]; # $employee1 = 'alice', $employee2 = 'carol' |
Simple, readable, and no loop required.
2. Array Slices
Array slices are just like list slices, but from an array variable:
1 2 |
my @subset = @employees[0, 2, 4]; # @subset = ('alice', 'carol', 'eve') |
You can also assign into an array slice to update multiple elements:
1 2 |
@employees[1, 3] = ('beatrice', 'daniel'); # @employees = ('alice', 'beatrice', 'carol', 'daniel', 'eve') |
Handy for bulk updates without writing explicit loops.
3. Hash Slices
This is where some people start to raise eyebrows — but hash slices are perfectly valid Perl and incredibly useful.
Let’s grab departments for a few employees:
1 2 |
my @departments = @details{'alice', 'carol', 'eve'}; # @departments = ('Engineering', 'HR', 'Sales') |
The @
sigil here indicates that we’re asking for a list of values, even though %details
is a hash.
You can assign into a hash slice just as easily:
1 |
@details{'bob', 'carol'} = ('Support', 'Legal'); |
This kind of bulk update is especially useful when processing structured data or transforming API responses.
4. Index/Value Array Slices (Perl 5.20+)
Starting in Perl 5.20, you can use %array[...]
to return index/value pairs — a very elegant way to extract and preserve positions in a single step.
1 2 |
my @indexed = %employees[1, 3]; # @indexed = (1 => 'bob', 3 => 'dave') |
You get a flat list of index/value pairs. This is particularly helpful when mapping or reordering data based on array positions.
You can even delete from an array this way:
1 2 |
my @removed = delete %employees[0, 4]; # @removed = (0 => 'alice', 4 => 'eve') |
And afterwards you’ll have this:
1 |
# @employees = (undef, 'bob', 'carol', 'dave', undef) |
5. Key/Value Hash Slices (Perl 5.20+)
The final type of slice — also added in Perl 5.20 — is the %hash{...}
key/value slice. This returns a flat list of key/value pairs, perfect for passing to functions that expect key/value lists.
1 2 |
my @kv = %details{'alice', 'dave'}; # @kv = ('alice', 'Engineering', 'dave', 'Engineering') |
You can construct a new hash from this easily:
1 |
my %engineering = (%details{'alice', 'dave'}); |
This avoids intermediate looping and makes your code clear and declarative.
Summary: Five Flavours of Slice
Type | Syntax | Returns | Added in |
---|---|---|---|
List slice | (list)[@indices] |
Values | Ancient |
Array slice | @array[@indices] |
Values | Ancient |
Hash slice | @hash{@keys} |
Values | Ancient |
Index/value array slice | %array[@indices] |
Index-value pairs | Perl 5.20 |
Key/value hash slice | %hash{@keys} |
Key-value pairs | Perl 5.20 |
Final Thoughts
If someone tells you that @hash{...}
or %array[...]
doesn’t work — they’re either out of date or mistaken. These forms are standard, powerful, and idiomatic Perl.
Slices make your code cleaner, clearer, and more concise. They let you express what you want directly, without boilerplate. And yes — they’re perfectly interview-appropriate.
So next time you’re reaching for a loop to pluck a few values from a hash or an array, pause and ask: could this be a slice?
If the answer’s yes — go ahead and slice away.