Using grep on undefined array
Andrew Beverley
andy at andybev.com
Wed Aug 14 00:09:16 BST 2013
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.
I've discovered that I can make it work by conditionally declaring
@fields (with "if $fields"), but I'd still like to know what's going on
here.
Thanks,
Andy
More information about the london.pm
mailing list