Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with writing "@ARGV ||= '.';"? [duplicate]

Tags:

perl

Why does Perl throw a fit at the following snippet?

$ perl -Mstrict -wE '@ARGV ||= ".";'
Can't modify array dereference in logical or assignment (||=) at -e line 1, near "'.';"
Execution of -e aborted due to compilation errors.

While it happily processes

$ perl -Mstrict -wE '@ARGV = @ARGV || ".";'

I don't see the perldiag explanation helping here much:

Can't modify %s in %s

(F) You aren't allowed to assign to the item indicated, or otherwise try to change it, such as with an auto-increment.


A more human-friendly explanation for this behavior is much appreciated.

like image 576
Zaid Avatar asked Jun 02 '13 14:06

Zaid


1 Answers

It's impossible for the code @ARGV to both return the array itself and the number of elements in it, so @ARGV ||= '.'; makes no sense. You need to evaluate @ARGV twice, once in scalar context (to get the number of elements), and once as an lvalue (to get the array itself).

 @ARGV = @ARGV || '.';
like image 170
ikegami Avatar answered Oct 13 '22 03:10

ikegami