I've stumbled upon this strange behavior: map will convert an undef scalar into an empty array reference when attempting to iterate on it. Here's an example:
Do you know if this behavior is documented somewhere? I've read the perlref manpage and its doesn't mention it. Could it be considered a bug in Perl?#!/usr/bin/perl -w use strict; use Data::Dumper; my $aref = undef; print 'BEFORE: ', $aref ? "TRUE\n" : "FALSE\n"; print Dumper $aref; print "\n"; my @a = map {$_} @$aref; print 'AFTER: ', $aref ? "TRUE\n" : "FALSE\n"; print Dumper $aref; print "\n";
One other interestinging thing about this is that the -w flag will complain when using an undefined value as an array reference, but not in the example above. Try substituting the map line above for "my @a = @$aref;" to see my point. I ran this on Perl 5.6.
Thanks,
Gregorovius
In reply to side effects of map by gregorovius
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |