Overview

Top Close Open

This design document proposes that the "Fat Arrow" => operator will be changed in TT3 to do (more-or-less) exactly the same thing that it does in Perl: to quote the word on the left.

Description

Top Close Open

TT2 does some magical handling of arguments passed to a subroutine or method call. All named parameters are collected in a hash array and passed as the last argument.

[% foo(10, 20, x=50, 30, 40, y=60) %]
[% foo(10, 20, x=>50, 30, 40, y=>60) %]   # same as above in TT2

The foo() subroutine receives the arguments as if they were written:

[% foo(10, 20, 30, 40, { x => 50, y => 60 }) %]

The problem is that the subroutine you call might not be expecting this. If you don't want the magical behaviour then you must explicitly pass all arguments as positional values.

[% bar('x', 10, 'y', 20) %]

sub bar {
    my %points = @_;
    # ...etc...
}

TT3 will continue to do it's magical thing when the = operator is used. However, when => is used it will now do the same thing that Perl does - to automagically quote a bareword on the left.

[% bar(x => 10, y => 20) %]     # TT3's => works like Perl so
[% bar('x', 10, 'y', 20) %]     # it's equivalent to this

You can mix and match the two if you're really sadistic.

[% mad(10, x => 20, plink = 50, 30, y => 40, plonk = 60) %]

That's the same as this:

[% mad(10, 'x', 20, 30, 'y', 40, { plink => 50, plonk => 60 }) %]

Rationale

Top Close Open

The => operator is a useful idiom in Perl. It confuses Perl programmers that it doesn't work the way that it does in Perl.

Impact

Top Close Open

You will no longer be able to treat => as a simple alias for = in an argument list. Hash definitions will continue to treat them both the same (as it does in TT2).

See Also

Top Close Open

TT3 Language Design Document 4 - Blessing Named Parameters


http://tt3.template-toolkit.org/docs/design/TT3DD03.html last modified 13:25:03 10-Dec-2009