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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:00:23+00:00 2026-05-24T08:00:23+00:00

Have a look at this piece of code: my @arr = (1, 2); my

  • 0

Have a look at this piece of code:

my @arr = (1, 2);
my $ref = \@arr;
my @s = @$ref;
push @s, 4;
print join(", ", @arr) . "\n";

Unexpectedly, the output is “1, 2”. What happened? How come I got two different arrays (@s is (1,2,4))?

Of course I get “1, 2, 4” if I write, before the output, something like this:

$ref = \@s;
@arr = @$ref;

But that seems rather clumsy.

I’m used to other OOP languages, in which such a thing wouldn’t happen – an object can me modified, regardless of its references.

So can anyone please help me with that?

  • 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-24T08:00:24+00:00Added an answer on May 24, 2026 at 8:00 am
    my @s = @$ref;
    

    makes a copy of the referenced array. After that, @s and @arr are unrelated arrays that just happen to have the same contents (for a while).

    If you want to modify the referenced array, you have to use it directly, e.g.:

    push @$ref, 4;
    

    There’s also the Data::Alias module, which (I think) lets you do what you’re asking for. But there’s deep magic involved there, and I’ve never used it myself.

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

Sidebar

Related Questions

I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I have this piece of code, when you look at it it would seem
Have a look at this piece of C++ code: class Foo { int a;
Let's have a look at this HTML code: <!DOCTYPE html> <html> <head> <title>Test</title> </head>
I have this piece of code from GWT in Action: public void processOperator(final AbstractOperator
Have a look at the following piece of code Header File: class CxUser {
I have this piece of code right here , I really don't get it
I have this piece of Jquery code that grabs a heading in a table
everyone, I have this piece of the code: void foo(int var, int var1) {
I have this piece of code that basically checks if one or more files

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.