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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:22:40+00:00 2026-06-07T03:22:40+00:00

I’ve just started learning perl and I am am confused by this exercise (from

  • 0

I’ve just started learning perl and I am am confused by this exercise (from Learning Perl Chapter 4).

At the beginning of the greet() subroutine, I am trying to assign the argument $_ to my $name (my $name = $_) variable but it doesn’t work. The book says to use “my name = shift;” but I don’t understand why. shift is used to remove a value from an array and my argument is not an array as far as I can tell, it’s a string inside a scalar!

Could anyone explain what I’m not understanding?

Thanks! Here is the entirety of the code.

use 5.012;
use warnings;
use utf8;

sub greet {
   my $name = $_;
   state $last_person ;
   if (defined $last_person ) {
          print "Hi $name! $last_person is also here!\n";

   } else {
          print "Hi $name! You are the first one here!\n";
   }
   $last_person = $name;
}

greet( 'Fred' );
greet( 'Barney' );
greet( 'Wilma' );
greet( 'Betty' );
  • 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-07T03:22:44+00:00Added an answer on June 7, 2026 at 3:22 am

    In chapter 4 of Learning Perl (6th edition) there’s a section called Arguments. There it states the following:

    This means that the first subroutine parameter is in $_[0], the
    second one is stored in $_[1], and so on. But – and here’s an
    important note – these variables have nothing whatsoever to do with the
    $_ variable, any more than $dino[3] (an element of the @dino
    array) has to do with $dino (a completely distinct scalar variable).
    It’s just that the parameter list must be in some array variable for
    your subroutine to use it, and Perl uses the @_ for this purpose.

    (Learning Perl, 6th Edition, Chapter 4)

    So you’re probably mistaken in using $_, when you should either be using my $name = $_[0];, or my $name = shift @_;. As a convenience, when you’re inside of a subroutine, shift defaults to shifting off of @_ if you provide no explicit argument, so the common idiom is to say my $name = shift;.

    For those in need of another resource, perldoc perlintro also has a good (and appropriately brief) explanation of passing parameters to subroutines and accessing them via @_ or shift.

    Here’s a brief snippet from perlintro:

    What’s that shift? Well, the arguments to a subroutine are available
    to us as a special array called @_ (see perlvar for more on that). The
    default argument to the shift function just happens to be @_ . So my
    $logmessage = shift;
    shifts the first item off the list of arguments
    and assigns it to $logmessage.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.