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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:34:27+00:00 2026-06-12T05:34:27+00:00

So while attempting to patch a bug recently, a fellow hacker told me that

  • 0

So while attempting to patch a bug recently, a fellow hacker told me that since the value of the string passed to the subroutine (method) might be quite large, accessing it via $_[1] would avoid the memory copy. However, I thought that any value passed to a subroutine was copied to @_ in the first place? so in the below example is the memory copied twice? or am I wrong about the copy made when passing to the method?

sub foo {
    my $self = shift

    $_[0]    # access $str in @_ directly
    my ( $str ) = @_; # makes another copy of @_
}

sub bar {
    my $self = shift;
    my $str = 'something very large'; 

    $self->foo( $str ); #copies $str to the @_ of foo
}

This is why I suggested to the author allowing pass by a scalar ref, which will avoid a copy (other than the reference itself ) when passing to the method itself. To reiterate: does passing a value to a subroutine mean the value gets copied into @_?

  • 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-12T05:34:29+00:00Added an answer on June 12, 2026 at 5:34 am

    According to http://perldoc.perl.org/perlsub.html (emphasis is mine):

    Any arguments passed in show up in the array @_ . Therefore, if you called a function with two arguments, those would be stored in $_[0] and $_[1] . The array @_ is a local array, but its elements are aliases for the actual scalar parameters. In particular, if an element $_[0] is updated, the corresponding argument is updated (or an error occurs if it is not updatable). If an argument is an array or hash element which did not exist when the function was called, that element is created only when (and if) it is modified or a reference to it is taken. (Some earlier versions of Perl created the element whether or not the element was assigned to.) Assigning to the whole array @_ removes that aliasing, and does not update any arguments.

    By my reading, this seems to indicate that, by default, no copy occurs into @_.

    Though I will admit that the language used is a little obtuse.

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

Sidebar

Related Questions

While attempting to debug a job submission script, I ended up narrowing down the
While attempting to create a replacement tablespace for USER, I accidentally created a datafile
While attempting to link with QT 4.5 release, I run into this error message
So my issues is that, for large groups of units, attempting to pathfind for
I'm attempting to create an array of strings that represent the directories stored in
While attempting to cross-compile ICU using android-ndk-r7 in Linux, the following error occurs after
I seem to be having some issues while attempting to implement logging into my
I am attempting to write (or expand on an existing) graph search algorithm that
I'm attempting to modify a mySQL query (that works) to return a more specific
I am attempting to use AFNetworking with an XML-RPC based API while using GDataXML

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.