develooper Front page | perl.perl5.porters | Postings from May 2008

Re: [perl #54974] Strange map {} Behavior When Returning Two Values

Thread Previous | Thread Next
From:
Graham Barr
Date:
May 30, 2008 17:36
Subject:
Re: [perl #54974] Strange map {} Behavior When Returning Two Values
Message ID:
60CB2FCC-7405-4026-8119-4F7851F5BBB8@pobox.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On May 30, 2008, at 9:27 AM, Abigail wrote:

> On Thu, May 29, 2008 at 03:16:31PM +0200, Aristotle Pagaltzis wrote:
>> * H.Merijn Brand <h.m.brand@xs4all.nl> [2008-05-29 13:00]:
>>> using a semi-colon
>>> $ perl -wle'my@a=map{;"--$_"=>$_}@ARGV;print"(@a)"' foo bar
>>> (--foo foo --bar bar)
>>>
>>> using parens
>>> $ perl -wle'my@a=map{("--$_"=>$_)}@ARGV;print"(@a)"' foo bar
>>> (--foo foo --bar bar)
>>>
>>> using a unary +
>>> $ perl -wle'my@a=map{+"--$_"=>$_}@ARGV;print"(@a)"' foo bar
>>> (--foo foo --bar bar)
>>>
>>> using catenation
>>> $ perl -wle'my@a=map{"--".$_=>$_}@ARGV;print"(@a)"' foo bar
>>> (--foo foo --bar bar)
>>>
>>> I personally think that using parens here to disambuigate is
>>> the most verbose option without adding unexplainable line noise
>>
>> Which in my mind makes it the least desirable, since that makes
>> it ???obviously??? superfluous and therefore the most likely to be
>> removed by some overeager maintainer. The semicolon, OTOH, is so
>> unusual that it is much more obviously intentional, even if the
>> maintainer who discovers it doesn???t know what it???s there for. It
>> probably won???t prevent them from removing it, but I would expect
>> that after the code breaks they will conclude that the semicolon
>> was put there purposely to prevent that error. With parens, in
>> contrast, it seems more likely to me that the maintainer will
>> think the code worked just by accident because the previous
>> programmer wrote it in a peculiar style with no particular
>> intent.
>
>
> I guess it's matter of style, but I prefer the parenthesis as  
> well.  The
> semi-colon is unual enough that it mystefies many people. The  
> overeager
> maintainer who removes the parenthesis will be left code that doesn't
> compile, let alone pass a test suite. I'm not so worried about that;
> it'll be a simple delete 2 chars, compile, undo cycle.  Worse would be
> a maintainer that wastes half a day what the hell the semi-colon is  
> for.

It is also one of the documented examples where the docs discuss the  
issue that started this thread

   %hash = map {  "\L$_", 1  } @array  # perl guesses EXPR.  wrong
   %hash = map { +"\L$_", 1  } @array  # perl guesses BLOCK. right
   %hash = map { ("\L$_", 1) } @array  # this also works
   %hash = map {  lc($_), 1  } @array  # as does this.
   %hash = map +( lc($_), 1 ), @array  # this is EXPR and works!

Graham.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Darwin)

iD8DBQFIQJZTR0BL4gbYw3QRAtKBAJ46jUPEdyv1xZWblpPFXNGnaysECQCgjw1U
uIcIbUhdpGdKRcmJvWPTDXw=
=U1eO
-----END PGP SIGNATURE-----

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About