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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:35:35+00:00 2026-05-13T00:35:35+00:00

I have to pass two references as arguments to a subroutine (buildRanges) as hash

  • 0

I have to pass two references as arguments to a subroutine (buildRanges) as hash key-value pairs as show below

Example:

@array = (“0A0”, “005”, “001”, “004”, “0BC”, “004”, “002”, “001”);
@ranges = ();
$numRanges = buildRanges(VALUES => \@array, REF_RANGES=>\@ranges);

My question is
1. is the syntax for the sub-routine call above is correct?
2. what are VALUES and REF_RANGES?

franckly speaking, i couldnt understand the sub-routine call, but i have been told to use that call only.

Thanks.

KK

  • 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-13T00:35:35+00:00Added an answer on May 13, 2026 at 12:35 am

    Explanations

    Yes. This syntax is correct and idiomatic for Perl. Let’s dissect all parts of your function call — most likely you already know parts of the explanation.

    Preparation: your input data

    @array = (“0A0”, “005”, “001”, “004”, “0BC”, “004”, “002”, “001”);
    @ranges = ();
    # \@array and \@ranges are now normal scalar references.
    

    I hope you understand the concept of references to arrays. If not, read perlreftut or perllol. Actually, this doesn’t affect the calling syntax that you are asking about. By the way, you might also have written:

    $array_ref=[“0A0”, “005”, “001”, “004”, “0BC”, “004”, “002”, “001”];
    $range_ref=[];
    
    $numRanges = buildRanges(VALUES => $array_ref, REF_RANGES=> $range_ref);
    

    The function call

    In Perl, the arrow operator => is working just like a normal comma (with a small side-effect that we discuss in a moment). The following calls are absolutely identical:

    $numRanges = buildRanges(VALUES => $array_ref, REF_RANGES => $range_ref);
    $numRanges = buildRanges("VALUES", $array_ref, "REF_RANGES", $range_ref);
    

    So, you are simply calling the function buildRanges with four arguments: two constant strings and two array references. You noticed that the word VALUES was changed to a constant string “VALUE”; the same applied to the word REF_RANGES. This is the special rule: before an arrow => and inside curly brackets {}, plain identifiers are silently converted to strings. We see this again below. But other expressions like $a => $b stay as they are, no silent conversions to strings happen here.

    You may be asking why does Perl do this? This is syntactic sugar: The => operator does nothing that you couldn’t do easily without it. But for experienced perl programmers the format KEY => $value looks clearer than “KEY”, $value.

    The function definition

    The definition of the function buildRanges may look like this (and we use this in our explanation):

    sub buidRanges { my(%info)=@_; 
      my $values    = $info{VALUES};
      my $ref_ranges= $info{REF_RANGES};
      my $special_feature = $info{SPECIAL_FEATURE}; # explained below
      if($special_feature) { ... return special_result; }
      ... process $values and $ref_ranges ... return $numRanges.
    }
    

    With your input data, each of the following four lines have the same effect:

    my(%info)=@_;                                          # the actual code
    my(%info)=(VALUES => \@array, REF_RANGES=> \@ranges ); # what this does
    
    my %info; $info{"VALUES"}=\@array; $info{"REF_RANGES"}=\@ranges; 
    my %info; $info{ VALUES }=\@array; $info{ REF_RANGES }=\@ranges; 
    

    Basically, the input data is converted into a hash that allows access to each argument. The constant strings in the function call become hash keys.

    Pick one line that you understand and compare it to the other lines: They do the same thing. Look at perldsc for more explanations.

    Note that @_ is the array of input arguments, in our case

    @_ = ( VALUES => \@array, REF_RANGES=> \@ranges); 
    @_ = ("VALUES",  \@array,"REF_RANGES", \@ranges);  # without syntactic sugar
    

    You may have noticed that $info{VALUES} is equivalent to $info{“VALUES”} — again, this is syntactic sugar, as explained above.

    Further down in our hypothetical implementation, we pull the input data out of the hash:

     my $values     = $info{VALUES};       # i.e:  my $value      = \@array;
     my $ref_ranges = $info{REF_RANGES};   # i.e:  my $ref_renges = \@ranges;
    

    Now, our function implementation can work with the input data.

    Why do we do this not simpler? — Named arguments

    So far, we could have achieved a similar effect much simpler:

    $numRanges = buildRanges($array_ref, $range_ref);  # simpler function call
    
    sub buidRanges { my($values, $ref_ranges)=@_; 
      ... process $values and $ref_ranges ... return $numRanges.
    }
    

    This is most likely the programming style that you already understand well.

    So: Why make it some Perl programmers more complicated (and also much slower)?
    The answer is: It’s more flexible, and somewhat more self-documenting.

    To make the point more clear, I added a SPECIAL_FEATURE to our function definition. Now, the function can also be called like this:

    $numRanges = buildRanges(VALUES => \@array, SPECIAL_FEATURE=> "infinite ranges");
    

    The implemented function can tell that the SPECIAL_FEATURE is requested and that the REF_RANGES are not provided.

    In some high-level functions, you have quite a number of extra features that are sometimes (but not always) useful, so it makes a lot of sense to let the caller decide which features to use and which not. Here, the named arguments come in handy.

    Of course, I can’t tell you what special features are recognized by your
    buildRanges function — you need to look at its implementation or ask the person that told you to use it. It’s also possible that, as a general convention, all high-level functions in some project use this calling style, even though some of them don’t provide much special features.

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

Sidebar

Related Questions

I have to pass parameters between two rails apps. In one side (sender) I
I have an application that uses simple sockets to pass some characters between two
I have to pass some parameter from an action to another action,for example to
I have around 8-9 parameters to pass in a function which returns an array.
I have some problems using two dimensional array. static const int PATTERNS[20][4]; static void
Suppose I have two classes: class A(): pass class B(): pass I have another
Do you have to pass delete the same pointer that was returned by new,
How do I pass have a Javascript script request a PHP page and pass
Most of the MVC samples I have seen pass an instance of the view
In C#, I am calling a SSIS package. I have to pass it 3

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.