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

  • SEARCH
  • Home
  • 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 7517969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:37:00+00:00 2026-05-30T01:37:00+00:00

A list assignment in scalar context returns the number of elements on the right

  • 0

A list assignment in scalar context returns the number of elements on the right hand side:

scalar(my ($hello, $there, $world) = (7,8)); #evaluates to 2

Why does it evaluate the right hand side and produce 2, instead of the newly defined list being evaluated and returning 3?

To me, it seems like $hello gets 7, $there gets 8, and $world gets undef, then that list is evaluated in scalar context, which would result in 3, as that is the number of elements in the list ($hello $there $world). It seems weird to me that context affects which part of the evaluated expression is returned:

my $greeting = (($hello, $there, $world) = (7,8)); #2

my @greeting = (($hello, $there, $world) = (7,8));
my $greeting_length = @greeting; #3
  • 1 1 Answer
  • 0 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-30T01:37:02+00:00Added an answer on May 30, 2026 at 1:37 am

    It’s documented to count the elements on the right in perlop (the last sentence in the Assignment Operators section):

    Similarly, a list assignment in list context produces the list of lvalues assigned to, and a list assignment in scalar context returns the number of elements produced by the expression on the right hand side of the assignment.

    The reason it works like that is so that you can write things like this:

    while (my ($key, $value) = each %hash) { ... }
    

    If it counted the number of elements on the left hand side of the assignment, that would be an infinite loop.

    If you think about it, the number of elements on the left hand side is either the same as on the right hand side or it’s a constant (when you’re assigning to a list of scalars). In the first case, it makes no difference which side you count, and in the second case, counting the right hand side is more useful.

    On the other hand, in list context the assignment operator returns the left hand list, because that’s more useful. If you use it in a context that modifies the list elements, you want to modify the variables that were just assigned to.

    Re: your comment In your example, (7,8) is a two-element list, which is why the assignment operator returns 2. When you assign a shorter list to a longer list of scalars, the right hand side is not “padded out” with undef before the assignment happens. Instead, any variables that did not have a value associated with them from the right hand list are reset to their default value. For a scalar variable, that’s undef. For arrays, that’s an empty array. For hashes, that’s an empty hash.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The standard says, under 5.17/9 A braced-init-list may appear on the right-hand side of
Does the overloading of the assignment operator propagate to an initializer list? For example,
Please write a list of tasks that a copy constructor and assignment operator need
For the new computer science assignment we are to implement a list/array using an
So basically the assignment was we had to create a doubly linked list that's
I was writing two implementations of a Linked List for an assignment, a Doubly
I need a little help with my new assignment. Problem: Given Two Linked List
In addition to implementing a Bag & List for an assignment, the next step
Background: my assignment was to write a program that uses linked list to keep
This is a class assignment to do ordered insertion into a sorted linked list.

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.