Vaguely, am I passing a scalar or array to a routine in perl?

I just started to learn perl, and I'm confused in this exercise (from Learning Perl Chapter 4).

At the beginning of the greet () routine, I am trying to assign the $ _ argument to the variable my $ name (my $ name = $ _), but this will not work. The book says: "My name = change"; but I don’t understand why . shift is used to remove the value from the array, and my argument is not an array, as far as I can tell, this is a string inside a scalar!

Can someone explain what I don't understand?

Thanks! Here is the complete 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' ); 
+4
source share
3 answers

Chapter 4 Learning Perl (6th Edition) has a section called Arguments . It says the following:

This means that the first parameter of the subroutine is in $_[0] , the second is stored in $_[1] , etc. But - and here's an important note - these variables have nothing to do with $_ , no more than $dino[3] (the @dino element of the array) has to do with $dino (a completely different scalar variable). It's just that the parameter list needs to be in some array variable for your routine to use it, and Perl uses @_ for this purpose.

( Exploring Perl, 6th Edition, Chapter 4 )

Thus, you are probably mistaken when using $_ , when you should either use my $name = $_[0]; , or my $name = shift @_; . For convenience, when you are inside a subroutine, shift by default switches from @_ if you did not specify an explicit argument, so the general idiom should say my $name = shift; .

For those who need another resource, perldoc perlintro also has a good (and accordingly short) explanation of passing parameters to subprograms and accessing them via @_ or shift .

Here is a short excerpt from perlintro :

What is this shift ? Well, there are arguments in the subroutine for us as a special array called @_ (for more details, see perlvar ). the default argument to the shift function is simply @_ . So my $logmessage = shift; shifts the first item from the argument list and assigns it to $logmessage .

+8
source

Looks like you skipped part of the chapter. You read the “Arguments” section (on page 66 in the last, sixth edition). It says:

Perl passes the list to the subroutine; that is, Perl makes the list available for the routine to use, but it is necessary. Of course, you should store this list somewhere, so Perl automatically saves the parameter list (another name for the argument list) into a special array variable named @_ for the duration of the subroutine. You can access this array to determine the number of arguments and the value of these arguments.

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

+4
source

The arguments of all perl routines live inside an array of parameters that your book doesn't mention.

This array is @_ , and in your case shift you will get your first argument, which will be a scalar.

+2
source

All Articles