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

I have hit a wall with the template system Text::Xslate. I can't find how to modify an array element or append to a hash or array. It keeps telling me it is forbidden.

I can do this: : my $arr=[1,2,3]; but I can not do this : $arr[1] = 12; Neither this: my $y = {a=>1,b=>2}; $y['c'] = 3; All is forbidden!

Do I miss something or is this kind of thing discouraged and I have to rethink it? But I need to transform even a little bit the data sent to the template.

Replies are listed 'Best First'.
Re: Text::Xslate : append to hash or array?
by choroba (Cardinal) on May 12, 2020 at 13:04 UTC
    This issue might be related.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Does not look promising. Reality check: is asking the template system to transform a bit (just a bit) the data, like adding another key-value pair to an input hash something that is discouraged? and my design is bad?

        Those questions are independent. In a pure View all action upon data is discouraged. Whether or not that is good or bad depends on one’s philosophy. I primarily use Text::Xslate now and, as you’ve found, it is quite irritating and unperly sometimes to have to be so careful about what to send it; it plays badly with some default DBIx::Class behavior for example. It is the a proverbial slippery slope between Text::Xslate which says, No! and Template::Toolkit which says, Yes, a million times, yes! No one really wants to hear …Maybe…

Re: Text::Xslate : append to hash or array?
by pryrt (Abbot) on May 12, 2020 at 14:26 UTC
    EDIT: my answer was irrelevant, as Text::Xslate does introduce new syntax. Sorry. Original post now mostly hidden by spoiler (leaving last line visible for easy context for the reply)


    I can do this: : my $arr=[1,2,3]; but I can not do this : $arr[1] = 12; Neither this: my $y = {a=>1,b=>2}; $y['c'] = 3; All is forbidden!

    I am assuming I have misunderstood your problem, but it seems to me, with what you've quoted, that your problem is irrelevant to Text::Xslate (which I know nothing about). If I try your code in pure perl, without Text::Xslate:

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my $arr=[1,2,3]; $arr[1] = 12; # Global symbol "@arr" requires explicit packa +ge name (did you forget to declare "my @arr"?) my $y = {a=>1,b=>2}; $y['c'] = 3; # Global symbol "@y" requires explicit package + name (did you forget to declare "my @y"?) print Data::Dumper->Dump([$arr, $y], [qw/arr y/]); __END__ Global symbol "@arr" requires explicit package name (did you forget to + declare "my @arr"?) at C:\usr\local\share\PassThru\perl\perlmonks\11 +116710.pl line 7. Global symbol "@y" requires explicit package name (did you forget to d +eclare "my @y"?) at C:\usr\local\share\PassThru\perl\perlmonks\111167 +10.pl line 10. Execution of C:\usr\local\share\PassThru\perl\perlmonks\11116710.pl ab +orted due to compilation errors.

    Since $arr is an array-ref and $y is a hash-ref, did you really mean the two assignments to be:

    $arr->[1] = 12; $y->{'c'} = 3;

    because if I run the following modified, it works as expected:

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my $arr=[1,2,3]; #$arr[1] = 12; # Global symbol "@arr" requires explicit pack +age name (did you forget to declare "my @arr"?) my $y = {a=>1,b=>2}; #$y['c'] = 3; # Global symbol "@y" requires explicit packag +e name (did you forget to declare "my @y"?) #print Data::Dumper->Dump([$arr, $y], [qw/arr y/]); # did you really mean: $arr->[1] = 12; $y->{'c'} = 3; print Data::Dumper->Dump([$arr, $y], [qw/arr y/]); __END__ $arr = [ 1, 12, 3 ]; $y = { 'a' => 1, 'c' => 3, 'b' => 2 };

    Or does Text::Xslate bring in some new syntax (or were those inside of text that Text::Xslate uses in a non-perlish way?) -- If that's the case, then feel free to ignore me.