A Slice of Perl

Sliced camel

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.

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:

You can also destructure directly:

Simple, readable, and no loop required.


2. Array Slices

Array slices are just like list slices, but from an array variable:

You can also assign into an array slice to update multiple elements:

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:

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:

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.

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:

And afterwards you’ll have this:

 

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.

You can construct a new hash from this easily:

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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.