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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:26:12+00:00 2026-05-15T05:26:12+00:00

I the code bellow line 9 creates a local copy of a hash. Any

  • 0

I the code bellow line 9 creates a local copy of a hash. Any changes to the %d will not provide changes to global %h variable (line: 5). I have to use reference (line: 8) to provide changes to %h.

Is there any way to dereference hash in a sub without creating a local copy?
I am asking since I have complicated record with many references and navigating around it with dereferences would be much easier.

  1 #!/usr/bin/perl -w
  2 use strict;
  3 use warnings;
  4 
  5 my %h;
  6 sub a {
  7 
  8     my $href = shift;
  9     my(%d) = %{$href};   # this will make a copy of global %h
 10     
 11     $$href{1}=2;     # this will make a change in global %h
 12     $d{2}=2;         # this will not a change in global %h
 13 }   
 14 a(\%h);
 15 print scalar (keys %h) . "\n";

—————-

Thanks for the replies.

The question is can I make some kind of “alias/bind” to %h in a sub.
I would like to change the context of %h in a sub with %d.
Whenever I create %d he makes local copy of %h – is there any way to avoid that or do I have to use references all the time?

—————-

One more time 🙂 I know how $href’s work. I read tutorial / manuals / docs etc.
I didn’t find the answer there – I assume that this is not possible since it wasn’t written there, but who knows.

I want to accomplish such behavior:

  6 sub a {
  7     $h{"1"}=2;
  8 }

Which is equivalent to that:

  6 sub a {
  8      my $href = shift;
  11     $$href{1}=2;     # this will make a change in global %h
  11     $href->{1}=2;    # this will make a change in global %h

Now how to do that with the help of %d – is it actually possible?

6 sub a {
7        my %d = XXXXXXXXX
.. }

What should I put under XXXXXXXXX to point to %h without creating a local copy?

  • 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-15T05:26:12+00:00Added an answer on May 15, 2026 at 5:26 am

    To create a local alias of the value, you need to use Perl’s package variables, which can be aliased using the typeglob syntax (and local to scope the alias):

    #!/usr/bin/perl -w
    use strict;
    use warnings;
    
    my %h;
    
    sub a {
        my $href = shift;
    
        our %alias; # create the package variable (for strict)
    
        local *alias = $href;
            # here we tell perl to install the hashref into the typeglob 'alias'
            # perl will automatically put the hashref into the HASH slot of
            # the glob which makes %alias refer to the passed in hash.
            # local is used to limit the change to the current dynamic scope.
            # effectively it is doing:  *{alias}{HASH} = $href
    
        $$href{1}=2;     # this will make a change in global %h
        $alias{2}=2;     # this will also make a change in global %h
    }
    a(\%h);
    print scalar (keys %h) . "\n";  # prints 2
    

    This is a fairly advanced technique, so be sure to read the Perl docs on local and typeglobs so you understand exactly what is going on (in particular, any subs called from within the a subroutine after the local will also have %alias in scope, since local denotes a dynamic scope. The localization will end when a returns.)

    If you can install Data::Alias or one of the other aliasing modules from CPAN you can avoid the package variable and create a lexical alias. The above method is the only way to do it without additional modules.

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

Sidebar

Related Questions

The code below works. But if I comment out the line Dim objRequest As
I'm calling the code below. On the line (IDataReader dr = cmd.ExecuteReader()) sql barfs
I'm getting the following error on the line of code below: Syntax error (missing
currently i obtain the below result from the following C# line of code when
Given the code bellow, how do I style the radio buttons to be next
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code below is not working as expected to detect if it is in design
The code below gives an error: Property 'Int32 Key' is not defined for type
I have a very simple little piece of Lua code, which I wrote while
Code is below, when I hit the page with: http://ryan.local.testmode.com/timecrunch/bctimepost.ashx?project=2593059&person=2831215&date=04/16/2009&hours=2.5&case=555 it just sort of

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.