rovf has asked for the wisdom of the Perl Monks concerning the following question:
In the following code, the second call to use_hash fails:
The ouput of the program is as follows:use strict; use warnings; $|=1; sub empty_list { @{ [] } } sub use_hash { my (%h) = @_; print(join(',',keys %h),"\n"); } use_hash(foo1 => undef, bar1 => 5); print "not defined\n" unless defined ((empty_list)[0]); use_hash(foo2 => (empty_list)[0], bar2 => 5);
We see that the first use_hash works. We also see that (not surprising) an empty list indexed by [0] yields undef. I would have expected the 2nd call to use_hash being the same as the first: Instead of passing undef<c> explicitly to key <c>foo2, I "generate" it by taking the first element of an empty list. However, the latter does not work.bar1,foo1 not defined Odd number of elements in hash assignment at C:\tmp\emptylist_a.pl lin +e 12. foo2,5
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Empty List miracle (2)
by moritz (Cardinal) on Apr 29, 2010 at 09:40 UTC | |
by JavaFan (Canon) on Apr 29, 2010 at 10:07 UTC | |
by ikegami (Patriarch) on Apr 29, 2010 at 15:21 UTC | |
|
Re: Empty List miracle (2)
by Sandy (Curate) on Apr 29, 2010 at 14:20 UTC |