Why does Perl's strict mode allow you to dereference a variable with an undefined value in this foreach context but not in an assignment context? -
this code:
#!/usr/bin/perl use 5.18.0; use strict;  # part 1 $undef = undef; print "1 $undef\n"; foreach $index (@$undef) {   print "unreachable no crash\n"; } print "2 $undef\n";  # part 2 $undef = undef; @array = @$undef; print "unreachable crash\n"; outputs:
1  2 array(0x7faefa803ee8) can't use undefined value array reference @ /tmp/perlfile line 12. questions part 1:
- why dereferencing $undefin part 1 change$undefarrayref empty array?
- are there other contexts (other foreach) dereferencing $undefchange in same way? terminology describe generic such case?
questions part 2:
- why dereferencing $undefin part 2 fall afoul ofstrict?
- are there other contexts (other assignment) dereferencing $undeffall afoul ofstrict. terminology describe generic such case?
1) for() in perl puts operand "l-value context", therefore $undef being auto-vivified existence array (reference) 0 elements (see this relatively similar question/answer regarding l-value context). 
3) because you're trying coercively assign undefined value else in r-value context, , that's illegal under strict (nothing gets auto-vivified in context, you're not magically creating variable nothing in l-value operation).
as far question 2 , 4, there several other context, many think of off top of head. 2, map() comes mind, or other operation treats operand l-value.
Comments
Post a Comment