in reply to Text::Xslate : append to hash or array?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Text::Xslate : append to hash or array?
by bliako (Abbot) on May 12, 2020 at 14:43 UTC |