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 3985532
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:54:06+00:00 2026-05-20T05:54:06+00:00

Please could you explain this apparently inconsistent behaviour to me: use strict; my @a;

  • 0

Please could you explain this apparently inconsistent behaviour to me:

use strict;
my @a;

print "a" x 2; # this prints: aa
@a = "a" x 2; print @a; # this prints: aa

print ("a") x 2; # this prints: a
@a = ("a") x 2; print @a; # this prints: aa

Shouldn’t the last one print a single ‘a’?

Edit: Ok so now this is kind of making more sense to me:
“Binary “x” is the repetition operator…In list context, if the left operand is enclosed in parentheses or is a list formed by qw/STRING/, it repeats the list.” perlop

This is as clear as mud to me (binary x – why use the word binary? Is there a denary X?)
But anyway:
@a = (“a”) x 2 # seems to be in list context, in that we have an array at the beginning – an array is not a list, but it contains a list, so I think we probably have a list context, (not an array context although they might be synonymous).

i suppose the “left operand” is (“a”). (It’s either that or @a). perlop doesn’t say what an operand actually is, querying perldoc.perl.org gives “No matches found”, and googling gives “In computer programming, an operand is a term used to describe any object that is capable of being manipulated.” Like an array for instance.

So the left operand might be enclosed in brackets so maybe it should “repeat the list”. The list is either: ("a") x 2
or it is: ("a")

If we repeated ("a") x 2 we would get ("a") x 2 ("a") x 2. This doesn’t seem right.

If we type: print $a[1] we will get a single ‘a’, so “it repeats the list” means Perl turns ("a") x 2 into ("a", "a") so we effectively get @a=("a", "a")

However, print ("a") x 2 does not get turned into ("a", "a"). That’s because print is a “list operator” with a high precedence. So there we effectively get: (print ("a")) x 2

An array is a term so it also has a high precedence, but @a=stuff involves the assignation operator = which has a relatively low precedence. So it’s quite different from print.

  • 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-20T05:54:07+00:00Added an answer on May 20, 2026 at 5:54 am

    You are being bitten by a common Perl parsing gotcha. Your third statement print ("a") x 2 is parsed as:

    (print ("a")) x 2;
    

    You can add another set of parentheses to fix the parsing:

    print (("a") x 2);  # prints aa
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please could you explain this *apparently* inconsistent behaviour to me: use strict; sub a
Could anyone please explain this to me with an example? I am getting contradicted
Been working on this for a while now and I think I've finally cracked
I often see in PHP MVC applications an Apache RewriteRule that looks like this:
I was under the impression that you needed a web proxy to parse the
In Ullman's book of compilers, in shift reduce parsing, following definition of viable prefix
I have read another post/question regarding jquery variables and it was useful but I'm
I'm looking to make many duplicates, or clones, of my CCSprite that's been subclassed.
I have a basic label that is supposed to indicate to the user that

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.