Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 818651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:11:03+00:00 2026-05-15T02:11:03+00:00

Ok, I understand perl hash slices, and the x operator in Perl, but can

  • 0

Ok, I understand perl hash slices, and the “x” operator in Perl, but can someone explain the following code example from here (slightly simplified)?

sub test{
    my %hash;
    @hash{@_} = (undef) x @_;
}

Example Call to sub:

test('one', 'two', 'three');

This line is what throws me:

@hash{@_} = (undef) x @_;

It is creating a hash where the keys are the parameters to the sub and initializing to undef, so:

%hash:

‘one’ => undef,
‘two’ => undef,
‘three’ => undef

The rvalue of the x operator should be a number; how is it that @_ is interpreted as the length of the sub’s parameter array? I would expect you’d at least have to do this:

@hash{@_} = (undef) x scalar @_;
  • 1 1 Answer
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:11:03+00:00Added an answer on May 15, 2026 at 2:11 am

    To figure out this code you need to understand three things:

    The repetition operator. The x operator is the repetition operator. In list context, if the operator’s left-hand argument is enclosed in parentheses, it will repeat the items in a list:

    my @x = ('foo') x 3;  # ('foo', 'foo', 'foo')
    

    Arrays in scalar context. When an array is used in scalar context, it returns its size. The x operator imposes scalar context on its right-hand argument.

    my @y = (7,8,9);
    my $n = 10 * @y; # $n is 30
    

    Hash slices. The hash slice syntax provides a way to access multiple hash items at once. A hash slice can retrieve hash values, or it can be assigned to. In the case at hand, we are assigning to a hash slice.

    # Right side creates a list of repeated undef values -- the size of @_.
    # We assign that list to a set of hash keys -- also provided by @_.
    @hash{@_} = (undef) x @_;
    

    Less obscure ways to do the same thing:

    @hash{@_} = ();
    $hash{$_} = undef for @_;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand a piece of code in perl, but I am
Perl newbie here...I had help with this working perl script with some HASH code
In this question: Can someone explain how BCrypt verifies a hash? Ian Boyd writes
I don't really understand why the following piece of perl code #!/usr/bin/perl -w use
I am editing a Perl file, but I don't understand this regexp comparison. Can
I'm trying to understand a particular Perl code from vcake . Usually I find
I'm trying to understand someone else's Perl code without knowing much Perl myself. I
I don't really understand Perl, so I was wondering if someone could give me
I don't understand the last line of this function from Programming Perl 3e .
Can some one help me understand the output of this Perl program: use Data::Dumper;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.