Todd Chester has asked for the wisdom of the Perl Monks concerning the following question:
How do I do this in Perl 5?
curl -s "http://usa.kaspersky.com/files/?file=kis&program=one&lang=en& +track=pu_kiskis_usen" -w %{redirect_url}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: URL Redirect
by Corion (Patriarch) on Sep 04, 2016 at 18:29 UTC | |
If you want to eliminate the dependency on the curl executable, maybe take a look at Net::Curl (closest to Curl) or WWW::Mechanize (closer to emulating a real webbrowser) or HTTP::Tiny (make a simple HTTP request). | [reply] [d/l] |
|
Re: URL Redirect
by hippo (Archbishop) on Sep 04, 2016 at 11:49 UTC | |
You can run arbitrary commands with system:
| [reply] [d/l] |
|
Re: URL Redirect
by Todd Chester (Scribe) on Sep 06, 2016 at 01:34 UTC | |
Hi All, Thank you for the suggestions! I have never been able to figure out Net::Perl I should have been more specific. I use LWP::UserAgent a lot and would like to master redirect URL's with it. Problem: it just times out on me. And not the 20 seconds I asked for either. More like several minutes. And "$res->redirects" returns what looks like an reference address pointer I am doing something wrong. cat Url.Redirest.Test.pl
UrlRedirectTest.pl main::GetUrlRedirect requesting http://usa.kaspersky.com/files/?file=kis&program=one&lang=en&track=pu_kiskis_usen http://usa.kaspersky.com/files/?file=kis&program=one&lang=en&track=pu_kiskis_usen redirects to HTTP::Response=HASH(0x24e9808)->redirects RedirectUrl = <HTTP::Response=HASH(0x24e9808)->redirects> WebSite http://usa.kaspersky.com/files/?file=kis&program=one&lang=en&track=pu_kiskis_usen redirects to 1 | [reply] [d/l] |
by hippo (Archbishop) on Sep 06, 2016 at 09:12 UTC | |
and "$res->redirects" returns what looks like an reference address pointer I am doing something wrong You are trying to invoke a method call within a string. The method call will not execute and what you see displayed instead is the reference to the object ($res) on which the method would otherwise have been called. You could write it like this:
which would be a general solution. However, we can see from the documentation that calling redirects on an HTTP::Response object returns a list, not a scalar. How you treat that will depend upon what you actually want this line of code to display. And what you want is not at all clear to me from the code you have provided. Maybe take a step back and explain precisely what it is you are trying to achieve here? | [reply] [d/l] [select] |
|
Re: URL Redirect
by Todd Chester (Scribe) on Sep 06, 2016 at 11:01 UTC | |
Hi Hippo,
The response from curl is:
and that response is precisely what I want from LWP.
$ ./Url.Redirest.Test.pl | [reply] [d/l] [select] |
by hippo (Archbishop) on Sep 06, 2016 at 11:37 UTC | |
Here is a simplified script which prints the first redirect of a URL (because in the absense of any statement to the contrary this is what I think you are asking for).
I've used Google as the source because it works whereas your provided source has issues (which you could take up with them). I think that this is pretty clear and shows how the various methods may be used to obtain information of relevance to the programmer. Do read the documentation of the modules you are using. | [reply] [d/l] |
|
Re: URL Redirect
by Todd Chester (Scribe) on Sep 06, 2016 at 11:45 UTC | |
Thank you! I am about to try it. | [reply] |
by Corion (Patriarch) on Sep 06, 2016 at 11:47 UTC | |
HTTP::Response->redirects returns a list of redirects. You can either assign that list to an array or another list. The parentheses around $redirect tell Perl that you want $redirect to be seen as list as you in fact are only interested in the first redirect. | [reply] [d/l] [select] |
by Todd Chester (Scribe) on Sep 07, 2016 at 01:22 UTC | |
I am still confused as to what | [reply] |
by Corion (Patriarch) on Sep 07, 2016 at 06:52 UTC | |
|
Re: URL Redirect
by Todd Chester (Scribe) on Sep 06, 2016 at 11:59 UTC | |
Hi Hippo, | [reply] |
by hippo (Archbishop) on Sep 06, 2016 at 12:41 UTC | |
Please can you not start a new subthread when you are actually replying to someone else's post? See how each post has its own "reply" link on the right side? That's what you should be doing. The long time is taken on the redirected page. If you're not interested in that then make sure LWP never tries to load it. eg:
This returns in a second or two. I once more encourage you to read the documentation for LWP::UserAgent where all of this is explained. | [reply] [d/l] |
by Todd Chester (Scribe) on Sep 07, 2016 at 01:13 UTC | |
That did the trick. I was missing
| [reply] [d/l] |
|
Re: URL Redirect
by Todd Chester (Scribe) on Sep 06, 2016 at 12:29 UTC | |
Hi Hippo, | [reply] |