If you add
print "@_\n"; between
sub zivad { and
my $animal = shift;, perl will tell you exactly what is in
@_. In this case you'd see
mago (which comes from
print zivad('mago');).
%list can be accessed anywhere after my in the snippet you posted, and that's just what your subroutine is doing.
This is what happens step-by-step
- the subroutine call zivad('mago') puts "mago" into @_ and starts running the code in the subroutine zivad.
- my $animal = shift takes the first element of @_ (which will be "mago") and puts it in to $animal.
- my $list = $list{$animal} || die "no animal" does a few things:
- Looks up $animal in %list and returns the value associated with "mago" ("ia-ia").
- If the animal wasn't in %list, the script will stop (die) with the "no animal" error message.
- The value returned from %list is assigned to $list.
- return $list returns "ia-ia" to the print that displays it on the screen.