Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

does anyone know what is the character for end of string in Template Toolkit? I am trying to do a search and replace, in this example to remove the last ','. This doesn't seem to work:
[% "Blah Blah," | replace(',$', '') %]

thanks

Replies are listed 'Best First'.
Re: What is the character for end of string in template Toolkit?
by almut (Canon) on Jan 16, 2009 at 03:50 UTC

    The regular '$' works just fine for me (Perl 5.8.8 on Linux, TT-2.20 — just in case it matters):

    #!/usr/bin/perl use Template; my $template = Template->new(); my $input = q{[% "Blah, Blah," | replace(',$', '') %]}; $template->process(\$input) || die $template->error(); # output "Bla +h, Blah"
Re: What is the character for end of string in template Toolkit?
by CountZero (Bishop) on Jan 16, 2009 at 05:52 UTC
    [% "Blah Blah," | remove(',$') %]
    should work as well.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Thanks guys! You are right. However, my actual program has this in a string variable and it doesn't work if I have it like this:
      [% foo = 'Blah blah,' %] [% foo | replace(',$', '') %]

      Any idea why? Appreciate the help!
        Not true. It works fine.
        use Template qw( ); my $template = Template->new(); my $input = <<'__EOI__'; [% foo = 'Blah blah,' %] [% foo | replace(',$', '') %] __EOI__ $template->process(\$input) or die $template->error();
        Blah blah