Perl shift operator simple question

Marco A. picture Marco A. · May 21, 2011 · Viewed 8.6k times · Source

What's the purpose of the following two lines of perl??

my $host = shift || 'localhost';
my $port = shift || 200;

That should return localhost and port 10. What is the shift keyword??

Answer

DavidO picture DavidO · May 21, 2011

The first line shifts from either @_ or @ARGV (depending on where you are in the code), or in the absence of any contents in @_/@ARGV, assigns localhost to $host.

The second one should be self-explanatory now.

Have a look at the shift documentation for details.