DEV Community

Cover image for A Slice of Perl
Dave Cross
Dave Cross

Posted on • Originally published at perlhacks.com on

A Slice of Perl

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.

my @employees = qw(alice bob carol dave eve);

my %details = (
  alice => 'Engineering',
  bob => 'Marketing',
  carol => 'HR',
  dave => 'Engineering',
  eve => 'Sales',
);
Enter fullscreen mode Exit fullscreen mode

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:

my @subset = (qw(alice bob carol dave eve))[1, 3];
# @subset = ('bob', 'dave')
Enter fullscreen mode Exit fullscreen mode

You can also destructure directly:

my ($employee1, $employee2) = (qw(alice bob carol))[0, 2];
# $employee1 = 'alice', $employee2 = 'carol'
Enter fullscreen mode Exit fullscreen mode

Simple, readable, and no loop required.


2. Array Slices

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

my @subset = @employees[0, 2, 4];
# @subset = ('alice', 'carol', 'eve')
Enter fullscreen mode Exit fullscreen mode

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

@employees[1, 3] = ('beatrice', 'daniel');
# @employees = ('alice', 'beatrice', 'carol', 'daniel', 'eve')
Enter fullscreen mode Exit fullscreen mode

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:

my @departments = @details{'alice', 'carol', 'eve'};
# @departments = ('Engineering', 'HR', 'Sales')
Enter fullscreen mode Exit fullscreen mode

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:

@details{'bob', 'carol'} = ('Support', 'Legal');
Enter fullscreen mode Exit fullscreen mode

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.

my @indexed = %employees[1, 3];
# @indexed = (1 => 'bob', 3 => 'dave')
Enter fullscreen mode Exit fullscreen mode

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:

my @removed = delete %employees[0, 4];
# @removed = (0 => 'alice', 4 => 'eve')
Enter fullscreen mode Exit fullscreen mode

And afterwards you’ll have this:

# @employees = (undef, 'bob', 'carol', 'dave', undef)
Enter fullscreen mode Exit fullscreen mode

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.

my @kv = %details{'alice', 'dave'};
# @kv = ('alice', 'Engineering', 'dave', 'Engineering')
Enter fullscreen mode Exit fullscreen mode

You can construct a new hash from this easily:

my %engineering = (%details{'alice', 'dave'});
Enter fullscreen mode Exit fullscreen mode

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.


The post A Slice of Perl first appeared on Perl Hacks.

Top comments (0)