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' );