[p4perl] Return of some P4Perl API commands are either a HashRef or an ArrayRef of Hashes

Tony Smith tony at smee.org
Wed Apr 13 13:06:47 PDT 2005


Hi Terry

> I recently came accros an issue that I am not sure if it is intended 
> behaviour or not. If it is I would like to know if there are any ways 
> around it. Here is the problem:
> 
>     * I have a p4 object as a memeber variable of another object (in
>       this example $self).
>     * My p4 object is initialised correctly.
>     * When I execute the following call:
> 
> 
> my $openedFiles = $self->{p4}->Opened();
> 
> I get either 1 hash reference back, something like this:
> 
> $openedFiles->{action}
> $openedFiles->{change}
> $openedFiles->{client}
> $openedFiles->{clientFile}
> $openedFiles->{deoptFile}
> ....
> 
> However, if I have more than one files opened, then I get back a 
> reference to an array of hashes. Something like this:
> 
> $openedFiles->[0]->{action}
> $openedFiles->[0]->{change}
> $openedFiles->[0]->{client}
> ....
> 
> $openedFiles->[1]->{action}
> $openedFiles->[1]->{change}
> $openedFiles->[1]->{client}
> ....
> 
> My problem is that if I want to process the returned $openedFiles 
> variable I would typically do something like this:

P4::Run() returns are defined thus:

     An array of results in array context.
     undef in scalar context if result set is empty.
     A scalar result in scalar context if only one result exists.
     An array ref in scalar context if more than one result exists.

So, if you always want an array of results to process, simply use array 
context:

   my @openedFiles = $p4->Opened();

Hope that helps!

Tony



More information about the p4perl mailing list