Re: We know what you need, and we'll push it down your throat.

[prev] [thread] [next] [lurker] [Date index for 2006/12/22]

From: Juerd
Subject: Re: We know what you need, and we'll push it down your throat.
Date: 16:33 on 22 Dec 2006
Abigail skribis 2006-12-22 16:43 (+0100):
> > >    print sqrt(9) + 7;  # Prints 10.
> > >    print sqrt (9) + 7; # Prints  4.
> The space influences precedence.

That's a result, not the cause.

The cause is that the space tells Perl that () is NOT the function call
parens anymore.

The real source of this problem is that Perl wants to allow subroutine
calls without parens:

    print "foo";

which really means:

    print("foo");

but can in Perl 5 also be written as:

    print ("foo");

which is nice until you get used to:

    $foo = (3 + 4) * 5;

being 35, and eventually try:

    print (3 + 4) * 5;

and are bitten by this being the equivalent:

    print(3 + 4) * 5;

which prints 7, and multiplies the return value of print() by 5,
throwing the resulting 1*5==5 away.

In Perl 6, however, parens that don't touch the function name
intimately, are always considered grouping parens, so that:

    print (3 + 4) * 5;

will print 35, instead of 7.

Of course, languages that don't allow you to call functions without
parens, don't have this problem:

    print((3 + 4) * 5);

and can very easily allow whitespace before the opening paren:

    print ((3 + 4) * 5);

It's nice for all those people coming from C and Java, who started to
learn Perl.

> Now, the other example I gave (%hash {code} vs %hash{code}) is a parsing
> issue.

They are both parsing issues, not precedence issues.

In fact, it's exactly the same parsing issue: if you have whitespace,
the thing following the whitespace is never parsed as a postfix
operator.

> Now, you might wonder what was gained by having this monster. It enables
> you to leave of the parenthesis in an if statement. 

Both (equal) issues are to enable you to drop parens:

    print "foo"; instead of print("foo");

    if $foo == 5 { ... } instead of if ($foo == 5) { ... }

It should be relatively easy to modify Perl 6's grammar to force parens
on both, and allow whitespace in front of postfix ops. But it will bite
in other places. Pick your hate :)

> Perl goes from a formfree language to a language with syntactically
> whitespace for the benefit of making parenthesis around a conditional
> optional.

Perl has always had syntactic whitespace, just not in any strictly
defined, language-wide consistent, way.

> (So, what's 'grep {/foo/} @bar;' meaning in perl6?)

Syntax error. grep takes two arguments, you need a comma in between:
    
    grep {/foo/}, @bar

or one of these alternative syntaxes:

    @bar.grep({/foo/})
    
    @bar.grep:{/foo/}

    @bar ==> grep {/foo/}

    grep {/foo/} <== @bar

(Personally, I like some extra whitespace here:
    
    grep { /foo/ }, @bar

    @bar.grep( { /foo/ } )
    
    @bar.grep:{ /foo/ }

    @bar ==> grep { /foo/ }

    grep { /foo/ } <== @bar

Perl 6 still allows whitespace there ;)

I expect people to hate that there are now 5 syntaxes for grep :) 
Fire at will, but I absolutely love it.
-- 
korajn salutojn,

  juerd waalboer:  perl hacker  <juerd@xxxxx.xx>  <http://juerd.nl/sig>;
  convolution:     ict solutions and consultancy <sales@xxxxxxxxxxx.xx>

Ik vertrouw stemcomputers niet.
Zie <http://www.wijvertrouwenstemcomputersniet.nl/>;.
There's stuff above here

Generated at 03:02 on 01 Jan 2007 by mariachi 0.52