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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:58:40+00:00 2026-06-06T16:58:40+00:00

I am new to Perl, can anyone explain the following scripts for me please:

  • 0

I am new to Perl, can anyone explain the following scripts for me please:

#!/usr/bin/env perl
use strict;
use warnings;
sub f1($) { my ($v) = @_; print "f1 $v\n"; }
sub f2(@) { my ($v) = @_; print "f2 $v\n"; }
my $s = "ww";
my @a = ("xx", "yy", "zz");
f1 $s; f1 @a; f2 $s; f2 @a;

The output in my computer is :

f1 ww
f1 3
f2 ww
f2 xx     # why!!

Can anyone explain why the fourth output is xx? I thought it should be zz, since when array converts to scalar, it should be the last element of array.

  • 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-06T16:58:42+00:00Added an answer on June 6, 2026 at 4:58 pm

    No, with a statement such as:

    my ($v, $foo, $bar) = @_;
    

    $v will be assigned the first value in the @_ array, $foo the second, and so on. This is because the parentheses impose a list context. Any excess values will be ignored, unless one of your variables is an array, in which case it will slurp all remaining values.

    my ($v, @foo, $bar) = @_;   # wrong! $bar will never get any value
    

    $v will get the first value, @foo all the rest. $bar will be undefined.

    You may be thinking of assignment using a list:

    my $v = qw(a b c);
    

    But this is wrong, and will cause an error:

    Useless use of a constant (a) in void context at -e line 1.
    Useless use of a constant (b) in void context at -e line 1.
    

    This is because the LHS using scalar context, it will (more or less) be similar to:

    'a';
    'b';
    my $v = 'c';
    

    You may notice that if we impose list context by putting $v inside parentheses, we get a different result:

    my ($v) = qw(a b c);  # $v is now 'a'
    

    ETA: Regarding prototypes:

    In f1, what you see is that the array is forced into scalar context because the subroutine expects a scalar argument. That is why f1 with an array prints 3 (the size). When the prototype looks for an array, the array remains in default list context, and the assignment is done as per normal (as described above).

    As an extra note: prototypes have a very specific use, to make subroutines behave more like certain built-ins with regard to argument handling. Such as sort { code here } or push @array, $foo.

    If this is not what you are after, you should skip prototypes all together and simply write:

    sub f1 {
    ...
    }
    

    Documentation here

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

Sidebar

Related Questions

#!/usr/bin/env perl use warnings; use strict; use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' );
please look at the following code first. #! /usr/bin/perl package foo; sub new {
I am new to perl, and can't seem to find answers about this anywhere.
I was wondering how I can add a new directory in my perl settings
use strict; use warnings; open(FILE4,cool.txt); open(FILE6,>./mool.txt); $line = <FILE4>; while ($line ne ) {
I am new to perl and can't seem to find why this snippet is
I'm new to Perl and I understand you can call functions by name, like
As I understand (Perl is new to me) Perl can be used to script
What are the concepts that must be known by a new Perl developer for
Pretty new to Perl so there may be a very obvious solution here. I'm

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.