Using grep on undefined array

Matt Lawrence matt.lawrence at virgin.net
Wed Aug 14 08:10:26 BST 2013


On 14/08/2013 00:09, Andrew Beverley wrote:
> Hi,
>
> Could someone please explain to me why the following outputs an empty
> string rather than "*"?
>
> get();
> sub get($)
> {   my $fields = shift;
>      my @fields = grep $_ ne 'domain', @$fields;
>      my $select_fields = $fields ? join(',', map { 'users.' . $_ } @fields) : '*';
>      print "$select_fields\n";
> }
>
> I would have expected $fields to remain undefined, but it seems to be
> turning into an empty array during the grep.
>
Search "autovivification" to get an explanation of why this happens.

Also, what would you expect the result of get(['domain']) to be?

Matt


More information about the london.pm mailing list