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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:08:19+00:00 2026-06-17T13:08:19+00:00

I am trying to pass reference of a hash into a subroutine defined in

  • 0

I am trying to pass reference of a hash into a subroutine defined in another module in order to increase performance. In the subroutine in this other module reference is dereferenced as:

sub subRoutine{
    my $hash_ref = $_[0];
    my %hash = %$hash_ref;

    $hash{$a_key} = $a_value;
}

So this changes the value of that key in this module BUT it doesn’t change the value in the perl program where this subroutine is called. What is the reason and how can I avoid this? Is it correct to use references instead of passing hashed/returning hashed in order to improve performance?

Thanks in advance!
Best wishes!

  • 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-06-17T13:08:20+00:00Added an answer on June 17, 2026 at 1:08 pm

    Passing references is fine if you want to manipulate the original variable, as you do here. The problem is that you are creating a copy of the referenced hash right away, and operating on the copy.

    The line my %hash = %$hash_ref; is creating a new hash and copying all the key/value pairs from the original hash. When you change this copy it has no effect on the original.

    Just get rid of that line, and replace the line $hash{$a_key} = $a_value; with $hash_ref->{$a_key} = $a_value;. You are now referring to an element of the original hash, and changing it.

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

Sidebar

Related Questions

I'm trying to pass an NSString by reference but it doesn't work. This is
I am trying to pass a variable by reference into a void pointer in
I am trying to pass a reference to an attribute's writer method to another
I am trying to pass a member reference to a parameter of another method.
I am trying to pass a queue to another function in c in order
I'm trying to return a reference to an object, not pass it by value.
I'm trying to pass a reference to a variable and then update the contents
I'm trying to pass an object (a reference to the currently logged on user,
This question builds off of a previously asked question: Pass by reference multidimensional array
I'm trying to pass in a memory reference to a character in a string

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.