Perl shift operator simple question

后端 未结 5 1971
离开以前
离开以前 2021-01-19 03:33

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

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

That should return loc

5条回答
  •  清歌不尽
    2021-01-19 04:02

    If no argument is provided, shift will shift @ARGV outside a subroutine and @_ inside a subroutine – that is the argument array passed to either the main program or the subroutine.

    In your case, $host is assigned the first element of @ARGV (or @_, if the code is inside a sub) or 'localhost', if the element is false.

    This is a very common Perl idiom.

提交回复
热议问题