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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:07:03+00:00 2026-06-16T14:07:03+00:00

I am learning perl and understand that it is a common and accepted practice

  • 0

I am learning perl and understand that it is a common and accepted practice to unpack subroutine arguments using shift. I also understand that it is common and acceptable practice to omit function arguments to use the default @_ array.

Considering these two things, if you call a subroutine without arguments, the @_ can (and will, if using shift) be changed. Does this mean that calling another subroutine with default arguments, or, in fact, using the @_ array after this, is considered bad practice? Consider this example:

sub total { # calculate sum of all arguments
    my $running_sum;
    # take arguments one by one and sum them together
    while (@_) {
       $running_sum += shift;
    }
    $running_sum;
}

sub avg { calculate the mean of given arguments
    if (@_ == 0) { return }
    my $sum = &total; # gets the correct answer, but changes @_
    $sum / @_ # causes division by zero, since @_ is now empty
}

My gut feeling tells me that using shift to unpack arguments would actually be bad practice, unless your subroutine is actually supposed to change the passed arguments, but I have read in multiple places, including Stack Overflow, that this is not a bad practice.

So the question is: if using shift is common practice, should I always assume the passed argument list could get changed, as a side-effect of the subroutine (like the &total subroutine in the quoted example)? Is there maybe a way to pass arguments by value, so I can be sure that the argument list does not get changed, so I could use it again (like in the &avg subroutine in the quoted text)?

  • 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-16T14:07:04+00:00Added an answer on June 16, 2026 at 2:07 pm

    In general, shifting from the arguments is ok—using the & sigil to call functions isn’t. (Except in some very specific situations you’ll probably never encounter.)

    Your code could be re-written, so that total doesn’t shift from @_. Using a for-loop may even be more efficient.

    sub total {
      my $total = 0;
       $total += $_ for @_;
      $total;
    }
    

    Or you could use the sum function from List::Util:

    use List::Util qw(sum);
    
    sub avg { @_ ? sum(@_) / @_ : 0 }
    

    Using shift isn’t that common, except for extracting $self in object oriented Perl. But as you always call your functions like foo( ... ), it doesn’t matter if foo shifts or doesn’t shift the argument array.
    (The only thing worth noting about a function is whether it assigns to elements in @_, as these are aliases for the variables you gave as arguments. Assigning to elements in @_ is usually bad.)

    Even if you can’t change the implementation of total, calling the sub with an explicit argument list is safe, as the argument list is a copy of the array:

    (a) &total — calls total with the identical @_, and overrides prototypes.
    (b) total(@_) — calls total with a copy of @_.
    (c) &total(@_) — calls total with a copy of @_, and overrides prototypes.

    Form (b) is standard. Form (c) shouldn’t be seen, except in very few cases for subs inside the same package where the sub has a prototype (and don’t use prototypes), and they have to be overridden for some obscure reason. A testament to poor design.
    Form (a) is only sensible for tail calls (@_ = (...); goto &foo) or other forms of optimization (and premature optimization is the root of all evil).

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

Sidebar

Related Questions

I am learning Perl and wrote this script to practice using STDIN. When I
I learning Perl and I want to create a simple application that gets all
I am currently learning Perl. I have Perl hash that contains references to hashes
I am still learning socket programming (using Perl) but I have both options (
Okay, so I'm using perl to read in a file that contains some general
I'm learning Perl and building an application that gets a random line from a
I'm playing and learning Perl so that I can read log files. I want
I am new to perl, I am learning using O'reillys Learning Perl book. Unicode
I know from Learning Perl, 6th Ed. (ISBN: 978-1-449-30358-7) p.58 that ($x, $y) =
I'm trying to work through a small Perl learning project that requires reading 4

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.