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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:45:52+00:00 2026-06-09T07:45:52+00:00

The purpose is to only ammend the hashref argument being passed to the function,

  • 0

The purpose is to only ammend the hashref argument being passed to the function, but not modify the original hashref.

{
    my $hr = { foo => q{bunnyfoofoo},
               bar => q{barbiebarbar} };

    foo( { %$hr, baz => q{bazkethound} } );
}

sub foo {
   my $args = shift // {};
}

I think the above would work, but am not sure there is a better way (other than modifying the orginal ref before passing).

  • 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-09T07:45:54+00:00Added an answer on June 9, 2026 at 7:45 am

    Your code is as simple as it can be. You need to pass a hash and you don’t want to modify the existing hash, so you need to create a new hash. That’s what { } does.

    The underlying problem is foo takes a hash ref instead of a key-value list.

    sub foo {
       my %args = @_;
       ...
    }
    

    You can still do everything you did before:

    foo( foo => 'bunnyfoofoo', bar => 'barbiebarbar' );
    

    my $hash = {
       foo => 'bunnyfoofoo',
       bar => 'barbiebarbar',
    };
    foo( %$hash, baz => 'bazkethound' );
    

    There are two advantages.

    1. You don’t have to use { } all over the place.

    2. You can modify the argument safely.

      This is buggy:

      sub f {
         my $args = $_[0] // {};
         my $foo = delete($args->{foo});
         my $bar = delete($args->{bar});
         croak("Unrecognized args " . join(', ', keys(%$args)))
            if %$args;
         ...
      }
      

      This is ok:

      sub f {
         my %args = @_;
         my $foo = delete($args{foo});
         my $bar = delete($args{bar});
         croak("Unrecognized args " . join(', ', keys(%args)))
            if %args;
         ...
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any available C++ (or maybe C) function/class/library with only purpose to sanitize
My purpose is user enter only email or mobile number. I have the partial
I've got a method in my class only for testing purpose : private void
One recommends me the following code apparently only in .zshrc without explaining its purpose
I am trying to modify a simply chat application for learning purposes. The only
Purpose: I plan to Create a XML file with XmlTextWriter and Modify/Update some Existing
My app's only purpose is to make posts as Facebook pages, so we'd like
I have a program whose only purpose is to drive a java.awt.Robot in an
I have an activity whose only purpose is to launch an AlertDialog. My issue
Recently I made a program in c, which only purpose was to overflow the

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.