[Petal] New syntaxes

Fergal Daly fergal at esatclear.ie
Wed Jul 9 13:08:56 BST 2003


On Wednesday 09 July 2003 08:43, Jean-Michel Hiver wrote:
> > Not much new but this fixes a problem it had with taint mode + caching 
with 
> > Petal >= 0.95.
> 
> Congrats for this new release, don't forget to rename it to Petal::Fast
> at some point :)

Thanks. I guess I could do that when I'm happy enough with it, which won't be 
until we've thrashed out some syntax extensions. May as well start now.

There are 4 changes I've made or want to make and I want to see what people 
think

1 Explicit accessors: thing{key} - done
2 Expressions in method arguments: thing/method arg1 arg2/wibble - done
3 Another method syntax: thing/method(arg1, arg2) - not done
4 Alternate string: syntax : "hello there ${user/name}" and 'hello there 
${user/name}'

--------------
1 Explicit accessors

I've added explicit hash, array and method accessors using [], {} and (). If 
you know the data type you are accessing, these are much faster, the usual 
"/" accessor has to look at the type of the data and decide what to do based 
on that. Here is the difference in the code produced

thing{key} compiles to

$hash->{"thing"}->{"key"}

thing/key compiles to

do{
  ref(my $ref = (($hash)->{"thing"})) || die "Not a ref";
  Scalar::Util::blessed($ref) &&
    (
      UNIVERSAL::can($ref, "key") or UNIVERSAL::can($ref, "AUTOLOAD")
     ) ? $ref->key() : $ref->{"key"}
}

which is obviously slower and similarly for [] and ().

I think Jean-Michel prefers thing/{key} to thing{key} because it's less 
Perlish, I prefer it because it's more Perlish and because the / isn't really 
needed... Anyone else got any opinions?

Also added is more flexible arguments for method calls. Before you could do

thing/method --hello "world" and you would get something like

$hash->{thing}->method("hello", "world")

----------------
2 Expressions in method arguments

I've added the ability to use expressions as arguments so

thing/method foo up[0]

is something like

$hash->{thing}->method($hash->{foo}, $hash->{up}->[0]}


I think it would also be nice to allow

thing/method(arg1, arg2) as an alternative

--------------------------
3 Another method syntax

To be consistent with thing/method() and to be a little more Perlish, I'd like 
to add

thing/method(arg1, arg2)

as an alternative to

thing/method arg1 arg2

so then if you really wanted, you could do

thing/method1(arg1, arg2/method2(arg3))

this is impossible using the current syntax as you would have to write 
something like

thing/method1 arg1 arg2/method2 arg3

but you can't tell if arg3 is the first argument of method2() or the last 
method1()

4 Alternate string: syntax : 'hello there ${user/name}'

If you use "string:" there's no way to indicate when the string stops, so you 
can't use "string:" as an argument to a method. However if you could also 
specify a string with "" and '' the problem would be solved. This would allow 
you to change

<t petal:define="messge string:hello ${user}">
  <t petal:replace="thing/greet message style" />
</t>

to

<t petal:replace="thing/greet 'hello ${user}' style" />

or

<t petal:replace="thing/greet('hello ${user}', style)" />
----------------

So, there you have it. I'm not sure if people need these or want these so 
please let me know. I'm hoping Petal::Fast will become standard but that 
won't happen if I add a load of junk that nobody wants or need.

I've mentioned in the past about turning Petal from a lovely template language 
into a poor programming language but I don't think the extensions above to 
that. In fact they don't really do very much that's new, they just make it 
easier to do things you could already do and make it possible to do some 
things you could almost do but not quite.

Feedback?

F



More information about the Petal mailing list