[31039] in Perl-Users-Digest
Perl-Users Digest, Issue: 2284 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 19 21:09:48 2009
Date: Thu, 19 Mar 2009 18:09:13 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 19 Mar 2009 Volume: 11 Number: 2284
Today's topics:
Re: A small problem I can't quite work out. <sjmarshy@gmail.com>
Re: A small problem I can't quite work out. <jimsgibson@gmail.com>
Re: A small problem I can't quite work out. <sjmarshy@gmail.com>
Re: A small problem I can't quite work out. <m@rtij.nl.invlalid>
Re: A small problem I can't quite work out. <martien.verbruggen@invalid.see.sig>
Re: A small problem I can't quite work out. <uri@stemsystems.com>
Re: A small problem I can't quite work out. <1usa@llenroc.ude.invalid>
Re: A small problem I can't quite work out. <sjmarshy@gmail.com>
Re: A small problem I can't quite work out. <tadmc@seesig.invalid>
Re: A small problem I can't quite work out. <tadmc@seesig.invalid>
Re: How can I keep LWP::UserAgent from adding the http- <spasticgoblin@gmail.com>
Re: How do I determine within the program what page I'm <tadmc@seesig.invalid>
Re: How do I determine within the program what page I'm <spasticgoblin@gmail.com>
Re: How do I determine within the program what page I'm <spasticgoblin@gmail.com>
Re: How do I determine within the program what page I'm <spasticgoblin@gmail.com>
how to extract bunch of things from ( ) <syangs04@gmail.com>
Re: how to extract bunch of things from ( ) <tadmc@seesig.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 19 Mar 2009 13:09:08 -0700 (PDT)
From: sj <sjmarshy@gmail.com>
Subject: Re: A small problem I can't quite work out.
Message-Id: <13b22aba-3d0c-4ce3-b534-d53fcdec4c8b@w34g2000yqm.googlegroups.com>
Okay, fixed it myself *is embarassed*
tried to delete the thread, but did it wrong.
Oh gosh, I'm a complete n00b...Never mind.
------------------------------
Date: Thu, 19 Mar 2009 14:05:18 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: A small problem I can't quite work out.
Message-Id: <190320091405184785%jimsgibson@gmail.com>
In article
<4564fd2b-e959-4878-a929-0fda10f11cf9@b16g2000yqb.googlegroups.com>, sj
<sjmarshy@gmail.com> wrote:
> Sorry, I'm completely new to both programming and usenet, I'll attempt
> to pick it up as fast as possible.
> I *think* it's the 'special' and 'checkinput' subs that are having the
> problems...
Thank you for making the effort to help us help you.
>
> #use strict;
> #use warnings;
use strict and warnings are very valuable tools. However, they don't
work if they are commented out. Some people here will not look at your
code unless both of these are present.
>
> my $checked2;
> my $flag = 1;
> my $flag2 = 1;
>
> while ($flag) {
> print $flag;
> my $usec;
> if ($flag == 1) {
> $usec = start();
> } else {
> $usec = restart();
> }
> $flag = $flag + 1;
>
> my $use = checkinput ($usec);
>
> writechat ($use);
>
> printchatall ();
>
> if ($use =~ /exit\n/i) {
> $flag = 0;
> }
> }
>
> sub start {
> intro();
> my $in = input();
> $flag2 = 1;
> return $in;
> }
>
> sub restart {
> print "what would you like me to say?: \n";
> my $in2 = input();
> $flag2 = 1;
> return $in2;
> }
>
> sub input {
> my $input = <>;
> return $input;
> }
This routine can be simplified to:
sub input {
return <>;
}
or even
sub input {
<>;
}
but you might want to remove the newline at the end of the input string:
sub input {
my $line = <>;
chomp($line);
return $line;
}
>
> sub helprint {
> open (HELP,"< help.txt") || die("can't find help file: $!");
> while (<HELP>) {
> print $_;
> }
> close HELP;
> return 0;
> }
>
> sub checkinput {
> while ($flag2 >= 1) {
> my $checked2;
> my $test2 = shift;
This statement is inside a while loop. Therefore, it can be executed
more than once, depending upon the logic involved in setting the value
of $flag2. While your program logic is convoluted, I do believe that
this loop will iterate more than once unless the user has entered a
special string (empty line, 'help', 'clear', etc.). You have called
this routine with one argument. The first time through the loop, the
argument (the user input) will be assigned to $test2 and shifted off
the @_ array. However, the second time through the loop, the @_ array
will be empty and $test2 will be undefined. This is the source of your
"Uninitialized" warnings.
You need to rethink the logic behind your user input processing logic.
> my $checked;
> $checked = special($test2);
> unless ($checked =~ /\n/i || $checked =~ /help\n/i || $checked =~ /
> clear\n/i || $checked =~ /""/ || $checked =~ /exit\n/i) {
$checked =~ /\n/i tests for the presence of a newline character in
the user input. Since such a character will always be present, this
test will never succeed. If you are trying to test for an empty line,
you need to anchor the R.E. at the beginning of the string:
$checked =~ /^\n/;
(there is no need to test case-insentively here).
It is better to use the regular expression meta-character '$' to test
for end of the string, and if you chomp the input, then this becomes:
unless ($checked =~ /^$/;
You are checking user input for special values both here and below. You
should only be doing this in one place. Checking just once will
simplify and improve your program logic.
> $flag2 = 0;
> }
> $flag2 = $flag2 + 1;
> if ($flag2 >= 2) {
> $checked2 = special($checked);
> }
> }
> return $checked2;
> }
> sub special {
>
> my $test = (shift);
>
> if ($test =~ /^help\n$/i) {
> helprint ();
> my $newans = input();
> return $newans;
> } elsif ($test =~ /^exit\n$/i) {
> $flag = 0;
> return 0;
> } elsif ($test =~ /^clear\n$/i) {
> open (CLEAR,"> chatthing.txt") || die("can't clear: $!");
You are using the string 'chatthing.txt' three times in your program.
You should put this string in a variable and use the variable
throughout your program.
Using the three-argument version of open and lexically-scoped (my)
variables for file handles is recommended:
open( my $clear, '>', $outfile ) or die("Can't open $outfile: $!");
> print CLEAR "";
print $clear "";
however, printing an empty string is a no-operation.
> my $newans1 = input();
> return $newans1;
> }else {
> return $test;
> }
> return "/n"
> }
>
> sub intro {
> open (INTRO,"< intro.txt") || die("can't find the intro: $!");
> while (<INTRO>) {
> print $_;
> }
> close INTRO;
> }
>
> sub writechat {
> my $answer = shift;
> open (CHATW,">> chatthing.txt") || die("can't open datafile: $!");
> print CHATW $answer;
> close CHATW;
> }
>
> sub printchatall {
> open (CHATR,"< chatthing.txt") || die("can't read datafile: $!");
> while (<CHATR>) {
> print $_;
> }
> close CHATR;
> }
--
Jim Gibson
------------------------------
Date: Thu, 19 Mar 2009 14:31:05 -0700 (PDT)
From: sj <sjmarshy@gmail.com>
Subject: Re: A small problem I can't quite work out.
Message-Id: <60139859-00ba-403e-9c5f-aa8d1b0cf553@t3g2000yqa.googlegroups.com>
On Mar 19, 9:05=A0pm, Jim Gibson <jimsgib...@gmail.com> wrote:
> In article
> <4564fd2b-e959-4878-a929-0fda10f11...@b16g2000yqb.googlegroups.com>, sj
>
> <sjmar...@gmail.com> wrote:
> > Sorry, I'm completely new to both programming and usenet, I'll attempt
> > to pick it up as fast as possible.
> > I *think* it's the 'special' and 'checkinput' subs that are having the
> > problems...
>
> Thank you for making the effort to help us help you.
>
>
>
> > #use strict;
> > #use warnings;
>
> use strict and warnings are very valuable tools. However, they don't
> work if they are commented out. Some people here will not look at your
> code unless both of these are present.
>
>
>
>
>
> > my $checked2;
> > my $flag =3D 1;
> > my $flag2 =3D 1;
>
> > while ($flag) {
> > =A0 print $flag;
> > =A0 my $usec;
> > if ($flag =3D=3D 1) {
> > =A0 $usec =3D start();
> > } else {
> > =A0 $usec =3D restart();
> > }
> > $flag =3D $flag + 1;
>
> > my $use =3D checkinput ($usec);
>
> > writechat ($use);
>
> > printchatall ();
>
> > if ($use =3D~ /exit\n/i) {
> > $flag =3D 0;
> > }
> > }
>
> > sub start {
> > =A0 intro();
> > =A0 my $in =3D input();
> > =A0 $flag2 =3D 1;
> > =A0 return $in;
> > }
>
> > sub restart {
> > =A0 print "what would you like me to say?: \n";
> > =A0 my $in2 =3D input();
> > =A0 $flag2 =3D 1;
> > =A0 return $in2;
> > }
>
> > sub input {
> > =A0 my $input =3D <>;
> > =A0 return $input;
> > }
>
> This routine can be simplified to:
>
> =A0 sub input {
> =A0 =A0 return <>;
> =A0 }
>
> or even
>
> =A0 sub input {
> =A0 =A0 <>;
> =A0 }
>
> but you might want to remove the newline at the end of the input string:
> =A0 sub input {
> =A0 =A0 my $line =3D <>;
> =A0 =A0 chomp($line);
> =A0 =A0 return $line;
> =A0 }
>
>
>
> > sub helprint {
> > =A0 open (HELP,"< help.txt") || die("can't find help file: $!");
> > =A0 while (<HELP>) {
> > =A0 =A0 print $_;
> > =A0 }
> > =A0 close HELP;
> > =A0 return 0;
> > }
>
> > sub checkinput {
> > =A0 while ($flag2 >=3D 1) {
> > =A0 =A0 my $checked2;
> > =A0 =A0 my $test2 =3D shift;
>
> This statement is inside a while loop. Therefore, it can be executed
> more than once, depending upon the logic involved in setting the value
> of $flag2. While your program logic is convoluted, I do believe that
> this loop will iterate more than once unless the user has entered a
> special string (empty line, 'help', 'clear', etc.). You have called
> this routine with one argument. The first time through the loop, the
> argument (the user input) will be assigned to $test2 and shifted off
> the @_ array. However, the second time through the loop, the @_ array
> will be empty and $test2 will be undefined. This is the source of your
> "Uninitialized" warnings.
>
> You need to rethink the logic behind your user input processing logic.
>
> > =A0 =A0 my $checked;
> > =A0 =A0 $checked =3D special($test2);
> > =A0 =A0 unless ($checked =3D~ /\n/i || $checked =3D~ /help\n/i || $chec=
ked =3D~ /
> > clear\n/i || $checked =3D~ /""/ || $checked =3D~ /exit\n/i) {
>
> =A0 $checked =3D~ /\n/i tests for the presence of a newline character in
> the user input. Since such a character will always be present, this
> test will never succeed. If you are trying to test for an empty line,
> you need to anchor the R.E. at the beginning of the string:
>
> =A0 $checked =3D~ /^\n/;
>
> (there is no need to test case-insentively here).
>
> It is better to use the regular expression meta-character '$' to test
> for end of the string, and if you chomp the input, then this becomes:
>
> =A0 unless ($checked =3D~ /^$/;
>
> You are checking user input for special values both here and below. You
> should only be doing this in one place. Checking just once will
> simplify and improve your program logic.
>
>
>
> > =A0 =A0 =A0 $flag2 =3D 0;
> > =A0 =A0 }
> > =A0 =A0 $flag2 =3D $flag2 + 1;
> > =A0 =A0 if ($flag2 >=3D 2) {
> > =A0 =A0 =A0 $checked2 =3D special($checked);
> > =A0 =A0 }
> > =A0 }
> > =A0 return $checked2;
> > }
> > sub special {
>
> > =A0 my $test =3D (shift);
>
> > =A0 if ($test =3D~ /^help\n$/i) {
> > =A0 =A0 helprint ();
> > =A0 =A0 my $newans =3D input();
> > =A0 =A0 return $newans;
> > =A0 } elsif ($test =3D~ /^exit\n$/i) {
> > =A0 =A0 $flag =3D 0;
> > =A0 =A0 return 0;
> > =A0 } elsif ($test =3D~ /^clear\n$/i) {
> > =A0 =A0 open (CLEAR,"> chatthing.txt") || die("can't clear: $!");
>
> You are using the string 'chatthing.txt' three times in your program.
> You should put this string in a variable and use the variable
> throughout your program.
>
> Using the three-argument version of open and lexically-scoped (my)
> variables for file handles is recommended:
>
> =A0 open( my $clear, '>', $outfile ) or die("Can't open $outfile: $!");
>
> > =A0 =A0 print CLEAR "";
>
> =A0 print $clear "";
>
> however, printing an empty string is a no-operation.
>
>
>
> > =A0 =A0 my $newans1 =3D input();
> > =A0 =A0 return $newans1;
> > =A0 }else {
> > =A0 =A0 return $test;
> > =A0 }
> > =A0 return "/n"
> > }
>
> > sub intro {
> > =A0 open (INTRO,"< intro.txt") || die("can't find the intro: $!");
> > =A0 while (<INTRO>) {
> > =A0 =A0 print $_;
> > =A0 }
> > =A0 close INTRO;
> > }
>
> > sub writechat {
> > =A0 my $answer =3D shift;
> > =A0 open (CHATW,">> chatthing.txt") || die("can't open datafile: $!");
> > =A0 print CHATW $answer;
> > =A0 close CHATW;
> > }
>
> > sub printchatall {
> > =A0 open (CHATR,"< chatthing.txt") || die("can't read datafile: $!");
> > =A0 while (<CHATR>) {
> > =A0 =A0 print $_;
> > =A0 }
> > =A0 close CHATR;
> > }
>
> --
> Jim Gibson
Cheers, that really helps a lot! I'll keep all that in mind. The
strict and warnings were commented out because I was trying to see
what difference it makes, I only just did it and forgot to undo it
before I posted. I'll make notes of all of this :)
------------------------------
Date: Thu, 19 Mar 2009 22:51:30 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: A small problem I can't quite work out.
Message-Id: <pan.2009.03.19.21.51.30@rtij.nl.invlalid>
On Thu, 19 Mar 2009 14:31:05 -0700, sj wrote:
> Cheers, that really helps a lot! I'll keep all that in mind. The strict
> and warnings were commented out because I was trying to see what
> difference it makes, I only just did it and forgot to undo it before I
> posted. I'll make notes of all of this :)
Keep in mind that people are really sensitive to such errors. Post the
exact code, with use strict and use warnings and the exact output from
the exact code you posted. Doing so will tremendously increase the
response from this group.
M4
------------------------------
Date: Fri, 20 Mar 2009 08:42:28 +1100
From: Martien Verbruggen <martien.verbruggen@invalid.see.sig>
Subject: Re: A small problem I can't quite work out.
Message-Id: <slrngs5f24.vhh.martien.verbruggen@mgjv.heliotrope.home>
On Thu, 19 Mar 2009 14:05:18 -0700,
Jim Gibson <jimsgibson@gmail.com> wrote:
> In article
><4564fd2b-e959-4878-a929-0fda10f11cf9@b16g2000yqb.googlegroups.com>, sj
><sjmarshy@gmail.com> wrote:
>
>>
>> sub input {
>> my $input = <>;
>> return $input;
>> }
>
> This routine can be simplified to:
>
> sub input {
> return <>;
> }
Except that the original always uses <> in a scalar context, while yours
uses <> in whatever context the input subroutine was used in.
Martien
--
|
Martien Verbruggen | Since light travels faster than sound,
first.last@heliotrope.com.au | is that why some people appear bright
| until you hear them speak?
------------------------------
Date: Thu, 19 Mar 2009 17:27:22 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: A small problem I can't quite work out.
Message-Id: <x77i2lrvw5.fsf@mail.sysarch.com>
>>>>> "JG" == Jim Gibson <jimsgibson@gmail.com> writes:
>> sub input {
>> my $input = <>;
>> return $input;
>> }
JG> This routine can be simplified to:
JG> sub input {
JG> return <>;
JG> }
JG> or even
JG> sub input {
JG> <>;
JG> }
not exactly. his will always return 1 line but yours could slurp in the
whole <> until eof if called in list context. but there is no need for
this sub to begin with.
JG> but you might want to remove the newline at the end of the input string:
JG> sub input {
JG> my $line = <>;
JG> chomp($line);
JG> return $line;
JG> }
now it becomes a slightly more useful sub but not really if it is only
called in 1 place.
JG> You need to rethink the logic behind your user input processing logic.
the OP is new at coding and stuck in the old way of using flags for
state. it will take some real world experience and time for him to
unlearn this.
JG> print $clear "";
JG> however, printing an empty string is a no-operation.
actually that would print "" to the handle in $clear! :)
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Thu, 19 Mar 2009 23:51:53 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: A small problem I can't quite work out.
Message-Id: <Xns9BD3CA1016A54asu1cornelledu@127.0.0.1>
Martijn Lievaart <m@rtij.nl.invlalid> wrote in
news:pan.2009.03.19.21.51.30@rtij.nl.invlalid:
> On Thu, 19 Mar 2009 14:31:05 -0700, sj wrote:
>
>> Cheers, that really helps a lot! I'll keep all that in mind. The
>> strict and warnings were commented out because I was trying to see
>> what difference it makes, I only just did it and forgot to undo it
>> before I posted. I'll make notes of all of this :)
>
> Keep in mind that people are really sensitive to such errors. Post the
> exact code, with use strict and use warnings and the exact output from
> the exact code you posted. Doing so will tremendously increase the
> response from this group.
This poster has already established a good example for others to follow
by responding positively to Tad's suggestions.
To the OP: It is a good idea to read the posting guidelines for this
group a few times. Also, Don't wait until you have a question to read
the documentation. Read the FAQ once every few months even though the
questions and the answers may not make sense yet. Take a look at perldoc
perltoc, and select topics that look interesting and/or relevant to what
you are doing.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Thu, 19 Mar 2009 17:26:41 -0700 (PDT)
From: sj <sjmarshy@gmail.com>
Subject: Re: A small problem I can't quite work out.
Message-Id: <efab9a00-a634-4369-b87a-8a1c288d3511@d19g2000yqb.googlegroups.com>
On Mar 19, 10:27=A0pm, Uri Guttman <u...@stemsystems.com> wrote:
> >>>>> "JG" =3D=3D Jim Gibson <jimsgib...@gmail.com> writes:
>
> =A0 >> sub input {
> =A0 >> my $input =3D <>;
> =A0 >> return $input;
> =A0 >> }
>
> =A0 JG> This routine can be simplified to:
>
> =A0 JG> =A0 sub input {
> =A0 JG> =A0 =A0 return <>;
> =A0 JG> =A0 }
>
> =A0 JG> or even
>
> =A0 JG> =A0 sub input {
> =A0 JG> =A0 =A0 <>;
> =A0 JG> =A0 }
>
> not exactly. his will always return 1 line but yours could slurp in the
> whole <> until eof if called in list context. but there is no need for
> this sub to begin with.
>
> =A0 JG> but you might want to remove the newline at the end of the input =
string:
> =A0 JG> =A0 sub input {
> =A0 JG> =A0 =A0 my $line =3D <>;
> =A0 JG> =A0 =A0 chomp($line);
> =A0 JG> =A0 =A0 return $line;
> =A0 JG> =A0 }
>
> now it becomes a slightly more useful sub but not really if it is only
> called in 1 place.
>
> =A0 JG> You need to rethink the logic behind your user input processing l=
ogic.
>
> the OP is new at coding and stuck in the old way of using flags for
> state. it will take some real world experience and time for him to
> unlearn this.
>
> =A0 JG> =A0 print $clear "";
>
> =A0 JG> however, printing an empty string is a no-operation.
>
> actually that would print "" to the handle in $clear! :)
>
> uri
>
> --
> Uri Guttman =A0------ =A0u...@stemsystems.com =A0-------- =A0http://www.s=
ysarch.com--
> ----- =A0Perl Code Review , Architecture, Development, Training, Support =
------
> --------- Free Perl Training ---http://perlhunter.com/college.html-------=
--
> --------- =A0Gourmet Hot Cocoa Mix =A0---- =A0http://bestfriendscocoa.com=
---------
Thanks for everyones help once again! I know it's a bit of a useless
program in reality but it's really helping me learn how to deal with
the basics, I'm learning a lot just from reading your replies. I
learned what I know thusfar about the concepts of programing from a
friend, in Visual Basic which he used to own, as well as bits and bobs
from the internet, which is probably why I'm a bit behind with my
techniques. I'll keep lurking here and pick up as much second hand
knowledge as I can :)
as for the code:
print $clear "";
I was using this to empty the file of contents, I don't know any other
way to do this so any advice would be appreciated.
Cheers again,
Sam
------------------------------
Date: Thu, 19 Mar 2009 18:43:26 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: A small problem I can't quite work out.
Message-Id: <slrngs5m4u.nfk.tadmc@tadmc30.sbcglobal.net>
sj <sjmarshy@gmail.com> wrote:
> On Mar 19, 9:05 pm, Jim Gibson <jimsgib...@gmail.com> wrote:
>> In article
>> <4564fd2b-e959-4878-a929-0fda10f11...@b16g2000yqb.googlegroups.com>, sj
>>
>> <sjmar...@gmail.com> wrote:
>> > Sorry, I'm completely new to both programming and usenet, I'll attempt
>> > to pick it up as fast as possible.
[ snip 200 lines ]
> Cheers, that really helps a lot! I'll keep all that in mind.
When composing a followup, quote only enough text to establish the
context for the comments that you will add.
Quoting 200 lines and adding 5 lines is seen as impolite.
Have you seen the Posting Guidelines that are posted here frequently?
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Thu, 19 Mar 2009 19:42:08 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: A small problem I can't quite work out.
Message-Id: <slrngs5pj0.ns4.tadmc@tadmc30.sbcglobal.net>
sj <sjmarshy@gmail.com> wrote:
> On Mar 19, 10:27 pm, Uri Guttman <u...@stemsystems.com> wrote:
>> --
>> Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com--
>> ----- Perl Code Review , Architecture, Development, Training, Support ------
>> --------- Free Perl Training ---http://perlhunter.com/college.html---------
>> --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com---------
Never quote a .signature (unless that is what you are commenting on).
Have you seen the Posting Guidelines that are posted here frequently?
> from a
> friend, in Visual Basic which he used to own,
Microsoft owns Visual Basic.
Your friend only licensed it from them.
> print $clear "";
>
> I was using this to empty the file of contents,
That could never have worked.
It was not that print() statement that emptied the file, it might
have been an open() that emptied it though.
> I don't know any other
> way to do this so any advice would be appreciated.
perldoc -f truncate
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Thu, 19 Mar 2009 15:31:48 -0700 (PDT)
From: CronJob <spasticgoblin@gmail.com>
Subject: Re: How can I keep LWP::UserAgent from adding the http-equiv strings from the Head section of the page?
Message-Id: <c04685d9-deb8-4c2f-ac68-d0edd57fc5fb@r31g2000prh.googlegroups.com>
Thank you Ben.
I ran 'perldoc LWP' and found:
The class name for the user agent is "LWP::UserAgent".
<snip>
=B7 The parse_head specifies whether we should initialize
response headers from the <head> section of HTML docu-
ments.
Running 'perldoc LWP::UserAgent' I see that:
$ua =3D LWP::UserAgent->new( %options )
This method constructs a new "LWP::UserAgent" object and
returns it. Key/value pair arguments may be pro-
vided to set up the initial state. The following options
correspond to attribute methods described below:
KEY DEFAULT
----------- --------------------
parse_head 1
I now realize that the 1 is implicitly a boolean value, and hence that
0 should do the trick for me.
Working code:
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use HTML::Parse;
use HTML::Element;
use HTTP::Response;
use HTTP::Request;
use HTTP::Status;
use URI::URL;
my $ie7UAString =3D 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0;
en-US)';
my ($code, $desc, $headers,$body) =3D &LWPUserAgentRequest('GET','http://
www.google.com');
print "The headers:\n$headers\n";
print "The body:\n$body\n";
sub LWPUserAgentRequest {
my ($method, $path) =3D @_;
my $ua =3D new LWP::UserAgent;
$ua->agent($ie7UAString);
$ua->parse_head(0);
my $request =3D new HTTP::Request($method, $path);
my $response =3D $ua->request($request);
my $body =3D $response->content;
$body =3D $response->error_as_HTML if ($response->is_error);
my $code =3D $response->code;
my $desc =3D HTTP::Status::status_message($code);
my $headers =3D $response->headers_as_string;
return ($code, $desc, $headers, $body);
}
------------------------------
Date: Thu, 19 Mar 2009 19:47:31 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: How do I determine within the program what page I'm on after LWP::UserAgent follows a redirect?
Message-Id: <slrngs5pt3.ns4.tadmc@tadmc30.sbcglobal.net>
CronJob <spasticgoblin@gmail.com> wrote:
>> > > 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)
>>
>> > 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 .
>
> 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?
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Thu, 19 Mar 2009 16:08:39 -0700 (PDT)
From: CronJob <spasticgoblin@gmail.com>
Subject: Re: How do I determine within the program what page I'm on after LWP::UserAgent follows a redirect?
Message-Id: <a0c5b474-44ec-4fc7-bbd8-100a3ee688bc@s38g2000prg.googlegroups.com>
>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.
------------------------------
Date: Thu, 19 Mar 2009 16:11:34 -0700 (PDT)
From: CronJob <spasticgoblin@gmail.com>
Subject: Re: How do I determine within the program what page I'm on after LWP::UserAgent follows a redirect?
Message-Id: <78b391c1-5de9-4f55-a02c-cacf2d0792eb@j18g2000prm.googlegroups.com>
> 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.
------------------------------
Date: Thu, 19 Mar 2009 16:35:00 -0700 (PDT)
From: CronJob <spasticgoblin@gmail.com>
Subject: Re: How do I determine within the program what page I'm on after LWP::UserAgent follows a redirect?
Message-Id: <df42f04e-d8ff-443b-aefb-65886988b3cd@u18g2000pro.googlegroups.com>
> > > 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)
>
> > 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 .
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.
------------------------------
Date: Thu, 19 Mar 2009 16:48:11 -0700 (PDT)
From: sherry <syangs04@gmail.com>
Subject: how to extract bunch of things from ( )
Message-Id: <532315a5-fd88-4647-9eea-f085aba51128@o2g2000prl.googlegroups.com>
I need to extract all the args from this line of code:
foo3( "something %s", ( BYTE )arg1, arg2.m_pcContentLocation );
the result is something like this:
"something %s", ( BYTE )arg1, arg2.m_pcContentLocation
I'm fairly new to perl. Not sure if this is doable.
Thanks for your help!
Sherry
------------------------------
Date: Thu, 19 Mar 2009 19:45:56 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: how to extract bunch of things from ( )
Message-Id: <slrngs5pq4.ns4.tadmc@tadmc30.sbcglobal.net>
sherry <syangs04@gmail.com> wrote:
> I need to extract all the args from this line of code:
>
> foo3( "something %s", ( BYTE )arg1, arg2.m_pcContentLocation );
>
> the result
The result of what?
Did you mean to post some code?
> is something like this:
>
> "something %s", ( BYTE )arg1, arg2.m_pcContentLocation
>
> I'm fairly new to perl. Not sure if this is doable.
perldoc -f split
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 2284
***************************************