Pearl shift operator is a simple question

What is the purpose of the following two lines of perl ??

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

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

+5
source share
5 answers

This piece of code is a way of providing default values ​​for $hostand $port. It is usually located at the beginning of a script or routine and takes values ​​from @ARGVand, @_respectively.

This should return localhost and port 10.

, || OR, , LHS , RHS . ( ): " , , ".

shift ARRAY ARRAY :

ARRAY , @_ @ARGV , eval STRING, BEGIN {}, INIT {}, CHECK {}, UNITCHECK {} END {} .

http://perldoc.perl.org/functions/shift.html

, , shift . shift , , .

+6

@_, @ARGV ( , ) @_/@ARGV, localhost $host.

.

. .

+9

, shift @ARGV @_ - , .

$host @ARGV ( @_, ) "localhost", .

Perl.

+3

shift . , .

+2
source

If you use shift, always put an array on it. I have seen Perl programmers forget that outside the routine shiftworks on @ARGV. The more things a programmer must remember at the same time, the greater the likelihood that he will make a mistake.

+2
source

All Articles