How do I determine within the program what page I'm on after

C

CronJob

How do I determine within the program what page I'm on after
LWP::UserAgent follows a redirect? Is there a way I can determine
whether redirects has occurred and if so how many redirects were
followed and what there urls were? (obviously I know what the first
one was)

Code snippet:

sub makeRequest( ) {
($method, $path) = @_;
# create a user agent object
my $ua = new LWP::UserAgent;
$ua->agent("Mozilla/4.0");

# request a url
my $request = new HTTP::Request($method, $path);
# set values in response object HTTP::Reponse
my $response = $ua->request($request);

# get the details if there is an error
# otherwise parse the response object
my $body=$response->content;
my $code=$response->code;
my $desc=HTTP::Status::status_message($code);
my $headers=$response->headers_as_string;
$body = $response->error_as_HTML if ($response->is_error);
return ($code, $desc, $headers, $body);
}

Thanks in advance for any insight.
 
T

Tad J McClellan

CronJob said:
sub makeRequest( ) {
^ ^
^ ^

Do you know what those parenthesis mean?

($method, $path) = @_;


Oh. That makes my earlier question rhetorical...

Your prototype says you take no args, but you take 2 args.

You should not use global variables like that. Use properly
scoped variables instead:

sub makeRequest {
my($method, $path) = @_;

my $ua = new LWP::UserAgent;
my $request = new HTTP::Request($method, $path);
my $response = $ua->request($request);


You use spaces around operators. That is a good style.

my $body=$response->content;
my $code=$response->code;
my $desc=HTTP::Status::status_message($code);
my $headers=$response->headers_as_string;


You do not use spaces around operators. That is an ungood style.

$body = $response->error_as_HTML if ($response->is_error);


Toggle the style back to using spaces around operators.

Thanks in advance for any insight.


Adopt a consistent programming style.
 
C

CronJob

                 ^ ^
                 ^ ^

Do you know what those parenthesis mean?


Oh. That makes my earlier question rhetorical...

Your prototype says you take no args, but you take 2 args.

You should not use global variables like that. Use properly
scoped variables instead:

    sub makeRequest {
        my($method, $path) = @_;


You use spaces around operators. That is a good style.


You do not use spaces around operators. That is an ungood style.


Toggle the style back to using spaces around operators.


Adopt a consistent programming style.

Thanks for taking the time to critique the program, but that isn't
what I was asking for and the example was cobbled together from
several sources. So discussion of the style wasn't really relevant to
my question (which I notice that you didn't even attempt to answer). I
guess you were so intent on how the trees were trimmed that you didn't
notice that you were in an orchard. But thanks for the pointer anyway.
I'll dereference at leisure.
 
C

CronJob

See the ->request and ->previous methods of HTTP::Response. ->request
returns the actual request that got this response, so you can get url of
this response with $reponse->request->uri .

Ben

Most helpful. I appreciate your response. Thank you for taking the
time to respond.
 
T

Tad J McClellan

Thanks for taking the time to critique the program, but that isn't
what I was asking for



This is a discussion group not a helpdesk. You post something
we discuss it's implications. If the discussion happens to
answer a question you've asked that's incidental.

my question (which I notice that you didn't even attempt to answer).


The carelessness evident in the code predisposed me to not bother
with an answer.
 
C

CronJob

Being rude to a regular who was trying to help you is not likely to win
you many friends here. This newsgroup is not a helpdesk: if you post
code, you must expect all manner of comments and critiques. If you don't
find a reply helpful, well, noone here is obliged to help you.
I wasn't being rude. I was trying to be funny, and wrote in earnest.
If you misunderstand my sense of humor I have been digesting the
comments on style. I really have no interest in being rude to anyone,
nor in fanning any sort of flames. I read through a number of "the
regular's" posts before I responded, and I attempted to use the same
dry/ironic sense of humor that I see in his posts.
Taking examples and playing with them is a perfectly good way to learn,
but once you get to the point of writing code that's supposed to be
useful you should understand what you're doing well enough to write for
yourself. Otherwise you've no hope of understanding what you wrote when
you come back to it later, and little hope of fixing it when it doesn't
do what you expect.

Yes, learning how to fish is much more significant than being handed a
fish. I would add that people learn in different styles. Some are
verbal, some are visual, and some are kinesthetic. We all have to play
the cards we are dealt.
 
C

CronJob

The carelessness evident in the code predisposed me to not bother
with an answer.

You of course are free to classify the example code any way you want.
I did learn some interesting things from your comments. So I thank you
for the time you spent looking over the code and commenting on it.
 
C

CronJob

How do I determine within the program what page I'm on after
I found that ->redirects gave me exactly what I was looking for.
Thanks for telling me what pond to look in and also where the fish
like to hang out.
 
T

Tad J McClellan

CronJob said:
I found that ->redirects gave me exactly what I was looking for.
Thanks for telling me what pond to look in and also where the fish
like to hang out.


Always indicate who wrote the quoted material.

Have you seen the Posting Guidelines that are posted here frequently?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top