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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:11:39+00:00 2026-05-27T11:11:39+00:00

I know this should be easily searchable on google, not to mention a trivial

  • 0

I know this should be easily searchable on google, not to mention a trivial use of perl, but I’ve tried many solutions I’ve found and so far none of them gives the expected behavior. Essentially, I’m trying to call a subroutine, return a reference to a hash from that subroutine, pass a reference to that hash to another subroutine, and then print the contents of that hash, via code similar to the following:

#!/usr/bin/perl                                                                                                                                                                                                   

my $foo = make_foo();

foreach $key (sort keys %$foo) {
    print "2 $key $$foo{$key}\n";
}

print_foo(\%foo);

sub print_foo
{
    my %loc = ???;
    foreach $key (sort keys %loc}) {
        print "3 $key $loc{$key}\n";
    }
}

sub make_foo
{
    my %ret;
    $ret{"a"} = "apple";
    foreach $key (sort keys %ret) {
        print "1 $key $ret{$key}\n";
    }
    return \%ret;
}

Can someone tell me the best way of doing this (via subroutines) without creating an extra copy of the hash? The solutions I’ve tried have not printed out any lines starting with “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-27T11:11:39+00:00Added an answer on May 27, 2026 at 11:11 am

    You have to pull the parameters in as a reference and then dereference it:

    sub print_foo
    {
        my ($loc) = @_;
        foreach my $key (sort keys %$loc) {
           print "3 $key $loc->{$key}\n";
        }
    }
    

    Anytime you make a reference, you have to explicitly dereference it. Also, beware that any changes to the reference will change the original. If you want to avoid changing the original, you can either pass the hash as a list:

    print_foo(%foo); # flattens the hash to a list and passes it in through @_
    
    sub print_foo
    {
        my (%loc) = @_; # load the hash from the list
        foreach my $key (sort keys %loc) {
           print "3 $key $loc{$key}\n";
        }
    }
    

    Or copy the hash reference into a new hash:

    sub print_foo
    {
        my ($ref_loc) = @_; # changes to %$ref_loc will change %foo
        my %loc = %$ref_loc; # COPY, changes to %loc do not change %foo
        foreach my $key (sort keys %loc}) {
           print "3 $key $loc{$key}\n";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this should be a basic question but I am hitting a brick
This is one of those I probably should know this, but I don't questions.
This should be very easy but I don't know how to do it. I
I know I should write this in Vb.net but for my own reasons I'm
I know I should be able to solve this myself, but I've been banging
This is something stupid I probably should know, but Googling fails me: When compiling
I know there should be a simple fix for this, but im braindead at
I know this is somewhat of a dead horse, but I'm not finding a
Context: OS: Linux (Ubuntu), language: C (actually Lua, but this should not matter). I
I know that this question has been asked too many times, but I think

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.