develooper Front page | perl.perl5.porters | Postings from December 2010

Re: [perl #80306] Quoting behaviour of => is too generous

Thread Next
From:
Ed Avis
Date:
December 9, 2010 05:34
Subject:
Re: [perl #80306] Quoting behaviour of => is too generous
Message ID:
loom.20101209T142757-306@post.gmane.org
Abigail <abigail <at> abigail.be> writes:


>>   foo(-width => 800, -height => 600);
>> 
>>How many Perl programmers, without looking at the Deparse output, would be able
>>to tell you that this parses as
>> 
>>    foo(-'width', 800, -'height', 600);

>>It looks odd, and Perl's behaviour of treating the unary minus operator as
>>a string operation prepending '-' is also strange to the uninitiated:
>
>Yeah, but because of this "strange" way unary minus acts, it's what makes
>module authors use the '-parameter' syntax. It also allows for
> 
>    foo (-width => 800, -height => 600, -autoscale);

My point is that the second of these behaviours (treating unary minus as a
string operation) is needed to compensate for the first (parsing -width as
-'width').  If the parsing were less surprising, then the semantics of unary
minus could be a bit more normal too.

The two peculiarities work together behind the scenes so that
foo(-width => 800, -height => 600) does what you expect, although by a rather
circuitous route.  But the rough edges are exposed when you make a typo and
leave out a comma somewhere; you then get a runtime warning for what (IMHO)
should be flagged as a syntax error.

It must be impossible to change the semantics of unary minus at this stage,
because lots of code depends on it.  However it's not clear that any real code
depends on the odd parsing of -width as -'width' instead of '-width'.

-- 
Ed Avis <eda@waniasset.com>


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