When a subroutine is called, the passed parameters are placed in a special @_ array. You can use this array by moving the values my $foo = shift or by directly assigning the array my ($foo,$bar) =@ _; . You can even use values ββdirectly from the array: $_[0]
Why is one against the other? Direct array assignment is the most standard and common. Sometimes the shift method is used when there are optional final values. Using a direct array is discouraged, with the exception of a few small niches: wrapper functions that call other functions, especially inside objects. functions that wrap other functions and change inputs. Also a special goto &func form that immediately removes the current call stack and calls func for the current @_ value.
# use shift for optional trailing values use v5.10; my $foo = shift; my $bar = shift // 'default bar value'; my $baz = shift // 'default baz value';
spazm source share