Re: Using grep on undefined array

2013-08-15 Thread Andrew Beverley
On Wed, 2013-08-14 at 00:09 +0100, Andrew Beverley wrote: Hi, Could someone please explain to me why the following outputs an empty string rather than *? [...] Thanks for all the replies - a very interesting read. I had never even heard of autovivification, so very useful to know what it

Re: Using grep on undefined array

2013-08-14 Thread Matt Lawrence
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.' .

Re: Using grep on undefined array

2013-08-14 Thread William Blunn
On 14/08/2013 00:09, Andrew Beverley wrote: Could someone please explain to me why the following outputs an empty string rather than *? In this case it may be wise to show a “use strict;”, which I assume is in effect here, otherwise people can just say that @$a on an undefined $a in the

Re: Using grep on undefined array

2013-08-14 Thread Mark Overmeer
* Andrew Beverley (a...@andybev.com) [130813 23:24]: 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 {

Re: Using grep on undefined array

2013-08-14 Thread Matt Freake
On Wed, Aug 14, 2013 at 8:40 AM, Mark Overmeer m...@overmeer.net wrote: * Andrew Beverley (a...@andybev.com) [130813 23:24]: Could someone please explain to me why the following outputs an empty string rather than *? @fields != @$fields, which is causing the confusion. I don't think the

Re: Using grep on undefined array

2013-08-14 Thread William Blunn
On 14/08/2013 01:17, Adrian Lai wrote: The use of @$fields is sufficient to autovivify $fields as an array ref. Hmmm. Ish. /In the context of a parameter to grep/ (and even this may be a misleading condition), the use of @$fields is sufficient to autovivify $fields as an array ref. But in

Re: Using grep on undefined array

2013-08-14 Thread Matt Lawrence
On 14/08/13 08:33, William Blunn wrote: On 14/08/2013 00:09, Andrew Beverley wrote: Could someone please explain to me why the following outputs an empty string rather than *? In this case it may be wise to show a “use strict;”, which I assume is in effect here, otherwise people can just say

Re: Using grep on undefined array

2013-08-14 Thread Mark Overmeer
* Matt Freake (matthew.d.fre...@gmail.com) [130814 08:01]: On Wed, Aug 14, 2013 at 8:40 AM, Mark Overmeer m...@overmeer.net wrote: * Andrew Beverley (a...@andybev.com) [130813 23:24]: Could someone please explain to me why the following outputs an empty string rather than *? @fields

Re: Using grep on undefined array

2013-08-14 Thread Abigail
On Wed, Aug 14, 2013 at 08:58:05AM +0100, Matt Freake wrote: On Wed, Aug 14, 2013 at 8:40 AM, Mark Overmeer m...@overmeer.net wrote: * Andrew Beverley (a...@andybev.com) [130813 23:24]: Could someone please explain to me why the following outputs an empty string rather than *?

Re: Using grep on undefined array

2013-08-14 Thread Matt Lawrence
On 14/08/13 13:12, Abigail wrote: On Wed, Aug 14, 2013 at 08:58:05AM +0100, Matt Freake wrote: I don't think the original poster is confusing those two, and when I applied 'use strict' to the original code (with the sub) it didn't help (no warnings or errors). The bit I'm stuck on is why does

Re: Using grep on undefined array

2013-08-14 Thread Adrian Lai
On 14 August 2013 13:12, Abigail abig...@abigail.be wrote: My guess: it's an unintentional side-effect of $_ being an alias in the first argument of grep. That does appear to be the case, given as other functions don't autovivify. e.g. perl -E '$y = scalar @$x; say $x if defined $x'

Re: Using grep on undefined array

2013-08-14 Thread James Laver
On Wed, Aug 14, 2013 at 1:59 PM, Matt Lawrence matt.lawre...@virgin.net wrote: man perlglossary: autovivification A Graeco-Roman word meaning “to bring oneself to life”. In Perl, storage locations (lvalues) spontaneously generate themselves as needed, including the creation of any hard

Re: Using grep on undefined array

2013-08-14 Thread Yitzchak Scott-Thoennes
On Wed, Aug 14, 2013 at 11:26 AM, James Laver james.la...@gmail.com wrote: perlglossary is wrong. It doesn't only occur as an lvalue but can occur by access as well. Yes and no. autovivification in general applies in writish kinds of context, but that is a somewhat imprecise concept. Note

Re: Using grep on undefined array

2013-08-14 Thread Avishalom Shalit
wait, aren't $a and $b special ? (they magically live for {$a=$b} etc. ) -- vish On 14 August 2013 04:07, William Blunn bill+london...@blunn.org wrote: On 14/08/2013 01:17, Adrian Lai wrote: The use of @$fields is sufficient to autovivify $fields as an array ref. Hmmm. Ish. /In the

Re: Using grep on undefined array

2013-08-14 Thread Abigail
On Wed, Aug 14, 2013 at 12:35:19PM -0400, Avishalom Shalit wrote: wait, aren't $a and $b special ? (they magically live for {$a=$b} etc. ) They are only special in special cases. Outside of that, they are as friendly as your other variable. People who pipe up upon seeing some code that uses

Re: Using grep on undefined array

2013-08-14 Thread Paul Makepeace
On Wed, Aug 14, 2013 at 9:35 AM, Avishalom Shalit avisha...@gmail.com wrote: wait, aren't $a and $b special ? (they magically live for {$a=$b} etc. ) IIRC, they're local'ised within the sort block. You can try it yourself, $ perl -wle 'print a=$a (pre init); $a = 5; @f = sort {print a=$a

Re: Using grep on undefined array

2013-08-14 Thread Uri Guttman
On 08/14/2013 01:03 PM, Abigail wrote: On Wed, Aug 14, 2013 at 12:35:19PM -0400, Avishalom Shalit wrote: wait, aren't $a and $b special ? (they magically live for {$a=$b} etc. ) They are only special in special cases. Outside of that, they are as friendly as your other variable. People who

RE: Using grep on undefined array

2013-08-14 Thread Eduardo Marques
Date: Wed, 14 Aug 2013 19:03:34 +0200 From: abig...@abigail.be To: london.pm@london.pm.org Subject: Re: Using grep on undefined array On Wed, Aug 14, 2013 at 12:35:19PM -0400, Avishalom Shalit wrote: wait, aren't $a and $b special ? (they magically live for {$a=$b} etc

Re: Using grep on undefined array

2013-08-14 Thread Abigail
On Wed, Aug 14, 2013 at 06:38:04PM +0100, Eduardo Marques wrote: Date: Wed, 14 Aug 2013 19:03:34 +0200 From: abig...@abigail.be To: london.pm@london.pm.org Subject: Re: Using grep on undefined array On Wed, Aug 14, 2013 at 12:35:19PM -0400, Avishalom Shalit wrote: wait, aren't

Re: Using grep on undefined array

2013-08-14 Thread Abigail
On Wed, Aug 14, 2013 at 01:26:05PM -0400, Uri Guttman wrote: On 08/14/2013 01:03 PM, Abigail wrote: On Wed, Aug 14, 2013 at 12:35:19PM -0400, Avishalom Shalit wrote: wait, aren't $a and $b special ? (they magically live for {$a=$b} etc. ) They are only special in special cases. Outside of

Using grep on undefined array

2013-08-13 Thread Andrew Beverley
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;

Re: Using grep on undefined array

2013-08-13 Thread Paul Makepeace
On Tue, Aug 13, 2013 at 4:09 PM, Andrew Beverley a...@andybev.com wrote: my $select_fields = $fields ? join(',', map { 'users.' . $_ } @fields) : '*'; my $select_fields = @fields ? join(',', map { 'users.' . $_ } @fields) : '*'; ? Maybe a lesson in variable naming there ;-) Paul

Re: Using grep on undefined array

2013-08-13 Thread Adrian Lai
On 14 August 2013 00:09, Andrew Beverley a...@andybev.com 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 ?

Re: Using grep on undefined array

2013-08-13 Thread Avleen Vig
On Aug 13, 2013 7:35 PM, Paul Makepeace pa...@paulm.com wrote: On Tue, Aug 13, 2013 at 4:09 PM, Andrew Beverley a...@andybev.com wrote: my $select_fields = $fields ? join(',', map { 'users.' . $_ } @fields) : '*'; my $select_fields = @fields ? join(',', map { 'users.' . $_ }

Re: Using grep on undefined array

2013-08-13 Thread Paul Makepeace
On Tue, Aug 13, 2013 at 4:09 PM, Andrew Beverley a...@andybev.com wrote: get(); sub get($) (You probably know this but calling get() like that, i.e. before it's declared, is denying perl the chance to enforce the subroutine prototype.)