1

I'm getting an error in Perl and I can't work out why.

Error: Not an ARRAY reference at Service.pm, line 20

my $array = [ { name => 'George', surname => 'Marley' } ];
my $helper = CustMessage->new();
$helper = CustMessage->getMessage($array);

then in my utility file I have:

sub getMessage {
    my ($self, $args) = @_;
    my $stringsArray = shift;

    my $strings = Service->new({
        serviceId => $self->_serviceId(),
    });

    return unless $strings;

    $strings->getStrings($stringsArray);
}

and then in the Service method is:

sub getStrings {
    my ($stringsArray, $self) = shift;
    my @keys = map({ $_->{'name'} } @{$stringsArray});
    my $key = join('', @keys); 
    $key = MIME::Base64::encode($key);

    my %results;
    $results{$key} = $self->_callStrings->($stringsArray);
    $results{$key}->initialize();

    $results{$key} = $self->{serviceCalls}->{getStrings};
    return $self->{serviceCalls}->{getStrings};
}

The error is on line 2 of the getStrings method in Service.pm:

my @keys = map({ $_->{'name'} } @{$stringsArray});
3
  • Missing , in my ($stringsArray $self) = shift;? Commented Dec 5, 2014 at 11:17
  • Nicely spotted but unfortunately that's not the issue. Think I accidentally removed that whilst copying and pasting (there's strange). Commented Dec 5, 2014 at 11:28
  • why is $helper an object first and then reassigned the value of the output of getMessage? I'm more used to seeing things like: my $helper = CustMessage->new(); my $msg = $helper->getMessage($array);. I guess if you don't use $helper again as an object then the reassignment works? Is this a typical pattern? Commented Dec 5, 2014 at 14:43

2 Answers 2

2

The lines

my $helper = CustMessage->new();
$helper = CustMessage->getMessage($array);

are very odd. You are creating a new CustMessage object in $helper and immediately discarding it and overwriting it with the result of

CustMessage->getMessage($array);

which, apart from any inheritance that may be going on, is identical to

getMessage('CustMessage', $array);

and I am suspecting that you don't have a real class defined as you call it your "utility file"

Because getMessage receives its arguments like this

my ($self, $args) = @_;
my $stringsArray = shift;

you are left with

($self, $args) = ('CustMessage', $array)
$stringsArray  = 'CustMessage'

and you never use $args again so your array reference is lost.

I cannot be sure what it is you actually want, because, as I said, I suspect that you don't have a proper CustMessage.pm file. But you could try

my $helper = CustMessage->new;
my $message = $helper->getMessage($array);

and then

sub getMessage {
    my ($self, $stringsArray) = @_;

    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sort of off topic for the OP (whose issue has been resolved I guess) but see my comment above regarding: my $helper = CustMessage->new(); ... is it just superfluous code?
@G.Cito: I'm not clear that the accepted answer catches all the errors in the question, but the OP seems happy. Take a look at my first paragraph -- you'll see that I agree with you about that line of code, although I'm not at all sure the OP has written a proper CustMessage.pm module as he calls it his “utility file”
1

RE: I'm getting an error in Perl and I can't work out why. Error: Not an ARRAY reference at Service.pm, line 20

Try

my ($self, $stringsArray) = @_;

instead of

my ($stringsArray, $self) = shift; 
# $self is always undef here due one element list assignment

since getStrings() is object method and object instance is always first element in @_ array.

1 Comment

Please comment on which part is wrong in above answer when down voting.

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.