[Petal] Modifiers and variables
Fergal Daly
fergal at esatclear.ie
Mon Aug 16 10:54:58 BST 2004
Last year when I was hacking Petal, I changed the modifier code so that it
examined the modifier at runtime to see if it was new style or old style.
For an old style modifier it just passed in the whole string to the process()
method as usual. If it was new style, it parsed the string a set of function
arguments and passed them to the new_process method. This allowed me to do
mod:this/that "some string"
(I think that'sargument syntax, it's been a while)
Looking at the current code in Petal/Hash.pm
sub fetch
{
my $self = shift;
my $key = shift;
my $mod = $self->_fetch_mod ($key);
$key =~ s/^\Q$mod\E//;
$key =~ s/^\s+//;
my $module = $MODIFIERS->{$mod} || confess "$mod is not a known modifier";
(defined $module and ref $module and ref $module eq 'CODE') and return $module->($self, $key);
$module->process ($self, $key);
}
just changing that last line to
if ($module->can("new_process")
{
# parse_args() is not correct but you get the idea
my @args = parse_args($key);
$module->new_process(@args);
}
else
{
$module->process ($self, $key);
}
would mean that no one has to parse TALES expressions in their modifiers,
Petal would do it itself and you could then do arbitrarily complex stuff in
the arguments to modifiers,
truncate:big/long/string max/string/length
F
On Sun, Aug 15, 2004 at 11:59:48PM -0400, William McKee wrote:
> Hey Jean-Michel et al,
>
> I'm working on adding several new modifiers to Petal::Utils and have
> come across an interesting proposal which I wanted to float with the
> list.
>
> When processing arguments passed to a modifier, there is not yet a
> standard way of reading these values (at least to my knowledge). Warren
> S. submitted a couple of modifiers which included a subroutine that he
> devised which fetches the arguments in the following way:
>
> 1 - if the argument is a number (decimals are OK) or contains single
> quotes, return the argument itself (i.e., plaintext)
> 2 - otherwise use the argument as the key to the hash and return that
> value
>
> I like this as it makes it really easy to pass in plain text without
> having to use the string: modifier. However, I'm not sure whether this
> is considered TAL compliant. What are your thoughts? I've included the
> subroutine at the end.
>
>
> Thanks,
> William
>
>
> sub _fetch {
> my ($hash, $a) = @_;
>
> return undef unless defined($a);
> if($a =~ /\'/) {
> $a =~ s/\'//g;
> return $a;
> }
> elsif($a =~ /^[0-9.]+$/) {
> return $a;
> }
> else {
> return $hash->fetch($a);
> }
> }
>
>
> --
> Knowmad Services Inc.
> http://www.knowmad.com
More information about the Petal
mailing list