arraysperlhashref

Perl - Issue getting array values from array stored in a hash reference


So I am currently working on a perl project where I need to pass an array (containing Id's to reject) stored as a hash reference, to another sub where I access the array use its contents in a json file I am creating. I currently wish to have the array stored as a ref in my main hash; "$self" which I am chucking between subs in order to access the token which I am using to gain auth. for the site I am working on:

#!/usr/bin/perl -I/usr/local/lib/perl
#all appropriate libaries used

#I use this to gen. my generic user agent, which is passed around.
my $hub = website->new( email=>'XXXX', password=>'XXXX'); 

# I use this to get the authorisation token, which is stored as a hash ref for accessing where needed (sub not shown)
my $token = $hub->auth(); 

#this is where I get the array of ids I want to reject
my @reject_array = $hub->approvelist(); 

# this is where the issue is happening as the array hash ref keeps just giving a total number of id's 
$hub->rejectcontacts; 


sub approvelist{
    my $self = shift;
    #$self useragent  and the token gets used in a http request to get json

    .
    #json code here to strip out the id for each contact in a for loop, which I then push into the array below  \/
    .
    .
    .
    push @idlist, $id;
    .
    .
    .
    #this is where I try to store my array as a hash 
    $self->{reject_array} = @idlist; 

    ref in $self 

    return @idlist;
}

sub rejectcontacts{
    #I do the same trick to try getting the useragent obj, the auth. token AND now the array
    my $self = shift; 
    .
    #code used for the HTTP request here...
    .
    .

    my $content = encode_json({
        reason=>"Prospect title",
        itemIds=>[@{$self->{reject_array}}], #this is where the problem is
    });

The problem is that when I attempt to access the array from the hash reference, I can only seem to get a scalar number of the number of elements within the array, rather than each of the actual contents in the array.

I have checked that the array is defined and the hash ref itself is defined, but when it comes to attempting to access the array in my "rejectcontacts" sub above, all I get is just the number of id's within it - 68 (the 68 figure gets put into the json file I am trying to write and then obviously doesn't work).

Any help on this would be greatly appreciated - I haven't been able to find a solution elsewhere.


Solution

  • Here:

    $self->{reject_array} = @idlist;
    

    You are assigning an array to a scalar. This results in the length of the array being assigned instead. The problem does not show up until you try to dereference the scalar, here:

    itemIds => [@{$self->{reject_array}}]
    

    You likely want:

    $self->{reject_array} = \@idlist;
    

    Also please note that this:

    itemIds => [@{$self->{reject_array}}]
    

    Could be simplified as:

    itemIds => $self->{reject_array}
    

    The only difference is that this does not copy the array, but as far as concerned this doesn't make a difference in your code. It will just make it more efficient.

    Reference: perldata:

    If you evaluate an array in scalar context, it returns the length of the array.