2

I'm having some problems creating a map with maps with arrays with maps. Its kind of complicated to explain, but it seems like it should be pretty easy to do. However, I'm having problems all over the place. Its going to be clear that I'm a perl-tard.

sub test_arg_maps {
  my %test_args;
  $test_args{"post_1"}{post} = q({"template_name": "unnamed_template_1", "template_subject":"unnamed_subject_1", "template_body":"body_1"});
  $test_args{"post_1"}{user} = q(x-idm test:[email protected]);
  $test_args{"post_1"}{save} = q(post_1);

  $test_args{"get_1"}{user} = q(x-idm test:[email protected]);
  $test_args{"get_1"}{save} = q(u0_template_get_1);

  $test_args{"u0_1"}{user}  = q(x-idm test:[email protected]);

  my @param_array = ();
  push(@param_array, (parameters => q(filter={"query":[]}), save => q(empty_filter)));
  push(@param_array, (parameters => q(filter={"query":[{"and":[]}]}), save => q(just_and)));
  $test_args{"u0_1"}{params} = @param_array;

  return %test_args;
}

The code that uses this function is:

my %test_args = test_arg_maps();
my @u0_1_array = $test_args{"u0_1"}{params};
for my $i (@u0_1_array)
{
    my %param_map = %{$i}; ## LINE 63
    $http->run(qq(
        /v1/template.aspx?$param_map{"parameters"}  -   Reject  $test_args{"u0_1"}{user}    ( callback => [qw(save_page $param_map{"save"} \$self->filter_json)]);
    ));
}

The error I get is:

ERROR at SIT::Harness (line 73): Can't use string ("4") as a HASH ref while "strict refs" in use at test.pl line 63.

EDIT: I've labeled line 63 above, its the first line in the for block.

I've tried several different ways to access these things, but they all give me errors which essentially tell me I don't have a hash where I'm looking for one.

Thanks for your help.

3
  • 1
    Building json by hand doesn't look quite appealing. Commented Nov 18, 2014 at 17:54
  • 1
    I can tell you it's not. Commented Nov 18, 2014 at 17:55
  • 3
    You can convert perl native array/hashes into json. metacpan.org/pod/JSON::XS Commented Nov 18, 2014 at 17:57

2 Answers 2

1

@param_array should be array of hashrefs,

push @param_array, {parameters => q(filter={"query":[]}), save => q(empty_filter)};
push @param_array, {parameters => q(filter={"query":[{"and":[]}]}), save => q(just_and)};
$test_args{"u0_1"}{params} = \@param_array;

and $test_args{"u0_1"}{params} is a reference to array,

my $u0_1_array = $test_args{"u0_1"}{params};
for my $i (@$u0_1_array) {
  my %param_map = %{$i}; ## LINE 63
}
Sign up to request clarification or add additional context in comments.

Comments

1

This line:

$test_args{"u0_1"}{params} = @param_array;

Is assigning the size of the array to that key, not the array itself. For that, you need to use a reference:

$test_args{"u0_1"}{params} = \@param_array;

In scalar context, an array returns its size, not its elements.

1 Comment

I did that, and I'm now getting "Not a HASH refernce at line 63." The same place that I had the error before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.