Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Subs calling themselves

by Bod (Parson)
on May 07, 2021 at 12:43 UTC ( [id://11132213]=perlquestion: print w/replies, xml ) Need Help??

Bod has asked for the wisdom of the Perl Monks concerning the following question:

Continuing from my previous discussion about connecting to LinkedIn - see Injecting a value into HTTP::Header

I have managed to authenticate with LinkedIn using LWP::Authen::OAuth2::ServiceProvider::LinkedIn which I am writing. This is used by LWP::Authen::OAuth2. LinkedIn doesn't obey the OAuth2 standard which is why there was a need to add extra information.

But now I am having difficulty posting anything to LinkedIn and I am getting an error from LWP::Authen::OAuth2
Can't call method "request" on an undefined value at /home/shoples1/perl5/lib/perl5/LWP/Authen/OAuth2.pm line 107.

I've looked at the source code of LWP::Authen::OAuth2 to try and see what I need to do differently to make this work. However, I cannot fathom how that module is supposed to work. Clearly, it does work and it is my understanding that is missing because it is a working module. But these two subroutines just seem to be calling each other:

sub request { my ($self, $request, @rest) = @_; return $self->access_token->request($self, $request, @rest); # <- +line 107 } sub access_token { my $self = shift; return $self->{access_token}; }
Having gone through the authentication and obtained a token, I am trying to make a post using the following code. I am not expecting it to actually make a post yet but I was expecting to get an error from LinkedIn and not an error from Perl.
my $params = { 'content' => { 'title' => 'Test Title', 'descrition' => 'Test message', }, }; my $res = $linkedin->post('https://api.linkedin.com/v2/shares', $param +s); if ($res) { print "Content-type: text/plain\n\n"; print $res; exit 0; }
Can you help me understand what the two subroutines in LWP::Authen::OAuth2 are actually doing?

Replies are listed 'Best First'.
Re: Subs calling themselves
by choroba (Cardinal) on May 07, 2021 at 12:51 UTC
    > these two subroutines just seem to be calling each other

    The access_token subroutine isn't calling any subroutine. It just accesses the hash referenced by $self and returns the value associated with the access_token key, i.e. it's a standard getter method.

    What does request do with the value? It calls the method request on it. It seems confusing, but the access token is probably of a different class than $self, so it's calling a different request method.

    Update: Indeed, see LWP::Authen::OAuth2::AccessToken.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Thank you choroba - it was the two different methods having the same name that was throwing me!

      Now to work out why $self->access_token is undefined...

Re: Subs calling themselves
by roboticus (Chancellor) on May 07, 2021 at 13:05 UTC

    Bod:

    The second function, access_token, is fetching a value from the object referenced by $self, so it's pretty straightforward.

    The request function is a *little* more subtle: As you notice, it calls the access_token function, but the result from that call is treated as a reference to an object that has a request function. Note that the object referenced by access_token is *not* necessarily the same class or object found in the request function you posted. So in order to understand what's happening, you'll need to figure out where the access_token member in the object is being set, so you can figure out what sort of object is being accessed.

    Sometimes when tracking through the code is a bit annoying, I'll do something like a quick edit of the module:

    $ # make a copy of the module so I don't mess it up $ cp /home/shoples1/perl5/lib/perl5/LWP/Authen/OAuth2.pm foo.pm $ vi /home/shoples1/perl5/lib/perl5/LWP/Authen/OAuth2.pm . . . edit the request subroutine to look like: sub request { my ($self, $request, @rest) = @_; use Data::Dumper; my $temp = $self->access_token; print "access token is: ", Dumper($temp), "\n"; return $temp->request($self, $request, @rest); } $ # run the project so I can see what I'm looking for $ perl t.pl blah, blah, blah access token is:$VAR1 = bless( { 'baz' => 'boom', 'foo' => 'bar' }, 'Barsoom!' ); blah, blah, blah $ # put things back so I don't cause more problems for myself $ cp foo.pm /home/shoples1/perl5/lib/perl5/LWP/Authen/OAuth2.pm

    Hope this helps!

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Hope this helps!

      Yes...it helps a lot. Thank you

      I need to work out why $self->access_token ends up being undefined and having a way to peek inside the module has to be helpful in working that out.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11132213]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found