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

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

Thread Previous | Thread Next
From:
Abigail
Date:
December 9, 2010 05:58
Subject:
Re: [perl #80306] Quoting behaviour of => is too generous
Message ID:
20101209135952.GE8108@almanda
On Thu, Dec 09, 2010 at 01:34:17PM +0000, Ed Avis wrote:
> 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'.

   sub foo;
   -foo;

That should parse as C<< - (foo ()) >>. If C<< -foo >> were
parsed as a single token, the above would be C<< "-foo" >>.

Of course, the above generates a warning (even if warnings are disabled),
so most people writes that anyway, opting for clearer C<< -+foo >> instead.
However, I start all my programs with "no warnings 'syntax'", which seems
to surpress the warning, hence, I may (and probably have) write C<< -foo >>,
expecting C<< foo () >> to be called.


Abigail

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