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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:37:08+00:00 2026-05-14T20:37:08+00:00

In my perl code I’ve previously used the following two styles of writing which

  • 0

In my perl code I’ve previously used the following two styles of writing which I’ve later found are being discouraged in modern perl:

# Style #1: Using & before calling a user-defined subroutine
&name_of_subroutine($something, $something_else);

# Style #2: Using ($$) to show the number of arguments in a user-defined sub
sub name_of_subroutine($$) {
  # the body of a subroutine taking two arguments.
}

Since learning that those styles are not recommended I’ve simply stopped using them.

However, out of curiosity I’d like to know the following:

  • What is the origin of those two styles of writing? (I’m sure I’ve not dreamt up the styles myself.)
  • Why are those two styles of writing discouraged in modern perl?
  • Have the styles been considered best practice at some point in time?
  • 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-14T20:37:09+00:00Added an answer on May 14, 2026 at 8:37 pm

    The & sigil is not commonly used with function calls in modern Perl for two reasons. First, it is largely redundant since Perl will consider anything that looks like a function (followed by parens) a function. Secondly, there is a major difference between the way &function() and &function are executed, which may be confusing to less experienced Perl programmers. In the first case, the function is called with no arguments. In the second case, the function is called with the current @_ (and it can even make changes to the argument list which will be seen by later statements in that scope:

    sub print_and_remove_first_arg {print 'first arg: ', shift, "\n"}
    
    sub test {
       &print_and_remove_first_arg;
    
       print "remaining args: @_\n";
    }
    
    test 1, 2, 3;
    

    prints

    first arg: 1
    remaining args: 2 3
    

    So ultimately, using & for every function call ends up hiding the few &function; calls which can lead to hard to find bugs. In addition, using the & sigil prevents the honoring of function prototypes, which can be useful in some cases (if you know what you are doing), but also may lead to hard to track down bugs. Ultimately, & is a powerful modifier to function behavior, and should only be used when that behavior is desired.

    Prototypes are similar, and their use should be limited in modern Perl. What must be stated explicitly is that prototypes in Perl are NOT function signatures. They are hints to the compiler that tell it to parse calls to those functions in a similar way as the built in functions. That is, each of the symbols in the prototype tells the compiler to impose that type of context on the argument. This functionality can be very helpful when defining functions that behave like map or push or keys which all treat their first argument differently than a standard list operator would.

    sub my_map (&@) {...}  # first arg is either a block or explicit code reference
    
    my @ret = my_map {some_function($_)} 1 .. 10;
    

    The reason sub ($$) {...} and similar uses of prototypes are discouraged is because 9 times out of 10 the author means “I want two args” and not “I want two args each with scalar context imposed on the call site”. The former assertion is better written:

    use Carp;
    sub needs2 {
       @_ == 2 or croak 'needs2 takes 2 arguments';
       ...
    }
    

    which would then allow the following calling style to work as expected:

    my @array = (2, 4);
    
    needs2 @array;
    

    To sum up, both the & sigil and function prototypes are useful and powerful tools, but they should only be used when that functionality is required. Their superfluous use (or misuse as argument validation) leads to unintended behavior and difficult to track down bugs.

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

Sidebar

Related Questions

I have the following Perl code, which reads the input and indents the file
Assume that the following Perl code is given: my $user_supplied_string = &retrieved_from_untrusted_user(); $user_supplied_string =~
I need to create Perl code which allows counting paragraphs in text files. I
I have written a perl code for processing file 'Output.txt' which has below Content.
In the following Perl code, I would expect to be referencing an array reference
Consider the following Perl code. #!/usr/bin/perl use strict; use warnings; $b=1; my $a=${b}; $b=2;
I'm trying to decrypt a Perl code which I'm not familiar with, somehow related
In the following perl code how to concatenate pwd with a string. In the
The following Perl code .. if ($^O eq MSWin32) { use Win32; .. do
I have a Perl code which goes to particular website and extracts the required

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.