gregorovius has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: side effects of map
by dvergin (Monsignor) on May 20, 2001 at 11:21 UTC | |
|
Re: side effects of map
by premchai21 (Curate) on May 20, 2001 at 08:48 UTC | |
|
Re: side effects of map
by rchiav (Deacon) on May 20, 2001 at 07:47 UTC |