[Templates] Syntactical Scalar call
Paul Seamons
mail at seamons.com
Thu Feb 28 16:09:45 GMT 2008
So - TT2 is plagued with the "all methods are called in scalar context" issue.
There have been recent patches to "fix" this, and there was even a new plugin
by Andy that provides for a scalar vmethod - though it requires a certain
level of voodoo to make it possible. The scalar vmethod is a neat hack - but
it is also a little ambiguous as we already have an item vmethod. Not only
that but it is the only vmethod that alters the chain that follows it.
In TT3, the plan is to merge filters and vmethods in-as-much as it is
possible. Alloy already has done this. The result is that | can be used
anywhere that a . is currently used. The only difference is that when a pipe
is used only vmethods or filters are searched for - no methods or hash values
are looked for.
Originally I was going to propose using | to mean to call the method in scalar
context - but then that allows method names to clobber virtual methods. I'd
propose making ! or some other character fulfill the purpose. Thinking about
it I would use the Perl 5 method call operator "->". Well - I started to
implemented it and i ran into -> used as a POST_CHOMP at the end of the Mason
tag. So -> won't work.
So with further thought - I think the following should be used:
[% foo.method(bar) %] # normal smart
[% foo.*method(bar) %] # same thing (mnemonic 0 or more)
[% foo.?method(bar) %] # scalar context (mnemonic 0 or 1)
[% foo.+method(bar) %] # full list context - always a list (mnemonic 1 or
more)
[% foo.!method(bar) %] # void context (this is optional)
I don't think that any real allowance needs to be made for top level
functions. Presumably - if you are able to pass in a code ref, you have a
little more access to it and can easily do sub { scalar mycode(@_) } instead
of \&mycode.
If a better proposal isn't furnished I will probably add this to
Template::Alloy. The scalar vmethod should already work if it follows the
standard implementation of TT plugins.
Just wondering about the future.
Paul
More information about the templates
mailing list