rovf has asked for the wisdom of the Perl Monks concerning the following question:
I tried 3 different ways to write a function returning an empty list.
For the print, the function is called in scalar context, so that the number of list elements should be printed. I thought the 3 variants are equivalent, but only for the first I get the expected value of 0. In the other two cases, Perl complains about uninitialized value being used. I understand that in the first case, the return expression is an empty array, while in the non-working case, it is an empty list, but I don't understand the implications here.use strict; use warnings; sub empty_list1 { @{ [] } } sub empty_list2 { qw,, } sub empty_list3 { () } $|=1; print(scalar(empty_list1()),"\n"); print(scalar(empty_list2()),"\n"); print(scalar(empty_list3()),"\n");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Empty List miracle(1)
by moritz (Cardinal) on Apr 29, 2010 at 09:28 UTC | |
by ikegami (Patriarch) on Apr 30, 2010 at 14:19 UTC | |
by chromatic (Archbishop) on Apr 29, 2010 at 23:37 UTC | |
by moritz (Cardinal) on Apr 30, 2010 at 06:28 UTC | |
by chromatic (Archbishop) on Apr 30, 2010 at 07:29 UTC | |
by Anonymous Monk on Apr 30, 2010 at 09:39 UTC | |
|
Re: Empty List miracle(1)
by JavaFan (Canon) on Apr 29, 2010 at 09:59 UTC | |
|
Re: Empty List miracle(1)
by Sandy (Curate) on Apr 29, 2010 at 13:46 UTC | |
by ikegami (Patriarch) on Apr 30, 2010 at 14:50 UTC | |
|
Re: Empty List miracle(1)
by ikegami (Patriarch) on Apr 30, 2010 at 14:42 UTC | |
by rovf (Priest) on May 03, 2010 at 12:58 UTC |