that is, it returns a hash when there is error, and a list reference when there is not.

I am bewildered, just how can I get this result into one variable and
then figure out if I am dealing with success (so that I can print
results from the list reference), or failure (so that I can print a
hash element error message).

Any idea? I tried obvious things like setting a scalar to the result
of this function, and could not.

The man pages gloss over this issue. I did read them in their entirety.

Advertisements

Ignoramus7272 <ignoramus7272@NOSPAM.7272.invalid> wrote in comp.lang.perl.misc:
> I am using PayPal API class
>
> Business:ayPal::API::TransactionSearch
>
> It uses some very weird code

Indeed. Snipped.
> that is, it returns a hash when there is error, and a list reference
> when there is not.
>
> I am bewildered, just how can I get this result into one variable and
> then figure out if I am dealing with success (so that I can print
> results from the list reference), or failure (so that I can print a
> hash element error message).
>
> Any idea? I tried obvious things like setting a scalar to the result
> of this function, and could not.

I don't know if you realise, but this will not call $sub as a method.
(Even if you pass in an object as the first argument manually, you will
not get any inheritance). You would have to call this function as

which is a little odd, to say the least. For my part, if I really wanted
a general-purpose sub like this (AFAICT, this is the only sub in
Business:ayPal that behaves like this, so I'd probably just wrap
that), I would either prototype it to accept a coderef and call that:

A quick scan of the docs for B:P::A::TS suggests that this behaviour is
a bug. Indeed, both the initial example and the "ERROR HANDLING" section
state that a succesful return *also* returns a hash, with a true 'Ack'
member and the result somewhere else (unspecified) in the hash. The 0.11
version says the module is still in alpha: Be Very Careful. Alpha
modules playing with your money is not something I would like. Depending
on how much work you want to do, you may be better off calling the
PayPal API yourself with SOAP::Lite.
> if ( @mystery == 1 ) {
> # normal processing
> return { success => 1, result => $mystery[0] };
> }
>
> # Failure
> my %hash = @mystery;
> return { success => 0, result => \%hash };

Here I would instead emulate the rest of the B:P::A interface, that is

Not that I like that interface one bit, but consistency is always a good
thing .

I would also consider overwriting &B:P::A::TS::TransactionSearch with
a sub that behaves correctly (perhaps with Hook::LexWrap); and
submitting a patch to the author.

Ben

--
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died. (Kate Rusby)

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!