For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2007 > how to make use of $content in LWP









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to make use of $content in LWP
wudenspoon@gmail.com

2007-09-14, 9:59 pm

I am looking for one particular line in an HTML page. How can I find
this in a perl program.

Basically, I want to grep this one line containing what I know.

Thanks.

RJ

Chas Owens

2007-09-14, 9:59 pm

On 9/14/07, wudenspoon@gmail.com <wudenspoon@gmail.com> wrote:
> I am looking for one particular line in an HTML page. How can I find
> this in a perl program.
>
> Basically, I want to grep this one line containing what I know.

snip

A lot depends on how you can identify that line. The best solution is
to use an HTML parser to create a Perl data structure you can walk
through to find your data; however, if you have distinct enough
anchors you can often easily grab the data with a regex (but this
solution tends to be fragile). Without more information about your
data we can't provide much more help.
W. Sp.

2007-09-17, 3:59 am

thanks Chas.

regex worked fine in my case. But my question was: how to specifically sift
out a particular line number. Also, while using LWP modules, what type of
data is $content = get($url)? Is it an array? Is there a way to find out
what kind of data a particular variable stores?

thanks
raghu

On 9/14/07, Chas Owens <chas.owens@gmail.com> wrote:
>
> On 9/14/07, wudenspoon@gmail.com <wudenspoon@gmail.com> wrote:
> snip
>
> A lot depends on how you can identify that line. The best solution is
> to use an HTML parser to create a Perl data structure you can walk
> through to find your data; however, if you have distinct enough
> anchors you can often easily grab the data with a regex (but this
> solution tends to be fragile). Without more information about your
> data we can't provide much more help.
>




--
There's a whole lot of difference between what one can do, what one thinks
one can do and what one does.

Jeff Pang

2007-09-17, 7:59 am

2007/9/17, W. Sp. <wudenspoon@gmail.com>:
> Also, while using LWP modules, what type of
> data is $content = get($url)? Is it an array? Is there a way to find out
> what kind of data a particular variable stores?


It's a scalar.
you can use 'ref' to find out the variable type,like,

$ perl -MLWP::Simple -e '$c=get "http://www.aol.com/";print ref \$c'
SCALAR
Chas Owens

2007-09-17, 7:00 pm

On 9/16/07, W. Sp. <wudenspoon@gmail.com> wrote:
snip
> regex worked fine in my case. But my question was: how to specifically sift
> out a particular line number.

snip


There is a global variable named $. that stores the current line
number. So you can say things like

perl -ne 'print $. if /this is found/' file.txt

If you want to print a line that is on a given line number you can say

perl -ne 'print if $. = 400' file.txt

Or if you want to print a range of lines you can use the flip-flop
operator (.. in scalar context)

perl -ne 'print if $. == 25 .. 50' file.txt

snip
> Also, while using LWP modules, what type of
> data is $content = get($url)? Is it an array? Is there a way to find out
> what kind of data a particular variable stores?

snip

Scalars hold strings, numbers, or references
Arrays hold multiple scalar values and allow indexing by position
Hashes hold multiple scalar values and allow indexing by key

If a scalar holds a reference then the ref function will tell you what
sort of reference it is (or its class if it is an object).
Chas Owens

2007-09-17, 7:00 pm

On 9/17/07, Jeff Pang <rwwebs@gmail.com> wrote:
> 2007/9/17, W. Sp. <wudenspoon@gmail.com>:
>
> It's a scalar.
> you can use 'ref' to find out the variable type,like,
>
> $ perl -MLWP::Simple -e '$c=get "http://www.aol.com/";print ref \$c'
> SCALAR

snip

You should probable not call ref in this way. If $c had held a
reference to an array it would have printed REF instead of ARRAY. Non
reference values return undef when passed to ref. If you see SCALAR
that means the value is a reference to a scalar. If you really want
to see output you should say something like this:

perl -MLWP::Simple -le '$c = get "http://3.am";print ref $c ? ref $c :
"SCALAR VALUE"'
Wagner, David --- Senior Programmer Analyst --- WG

2007-09-17, 7:00 pm

> -----Original Message-----
> From: Chas Owens [mailto:chas.owens@gmail.com]=20
> Sent: Monday, September 17, 2007 10:14
> To: W. Sp.
> Cc: beginners@perl.org
> Subject: Re: how to make use of $content in LWP
>=20
> On 9/16/07, W. Sp. <wudenspoon@gmail.com> wrote:
> snip
> specifically sift
> snip
>=20
>=20
> There is a global variable named $. that stores the current line
> number. So you can say things like
>=20
> perl -ne 'print $. if /this is found/' file.txt
>=20
> If you want to print a line that is on a given line number you can say
>=20
> perl -ne 'print if $. =3D 400' file.txt


$. =3D=3D 400 and not $. =3D 400 ( assignment verses equality test ).
Wags ;)

>=20
> Or if you want to print a range of lines you can use the flip-flop
> operator (.. in scalar context)
>=20
> perl -ne 'print if $. =3D=3D 25 .. 50' file.txt
>=20
> snip
> way to find out
> snip
>=20
> Scalars hold strings, numbers, or references
> Arrays hold multiple scalar values and allow indexing by position
> Hashes hold multiple scalar values and allow indexing by key
>=20
> If a scalar holds a reference then the ref function will tell you what
> sort of reference it is (or its class if it is an object).
>=20
> --=20
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>=20
>=20
>=20


****************************************
******************************
This message contains information that is confidential and proprietary to F=
edEx Freight or its affiliates. It is intended only for the recipient name=
d and for the express purpose(s) described therein. Any other use is proh=
ibited.
****************************************
******************************

Rob Dixon

2007-09-17, 7:00 pm

Chas Owens wrote:
>
> On 9/17/07, Jeff Pang <rwwebs@gmail.com> wrote:
>
> You should probable not call ref in this way. If $c had held a
> reference to an array it would have printed REF instead of ARRAY. Non
> reference values return undef when passed to ref. If you see SCALAR
> that means the value is a reference to a scalar. If you really want
> to see output you should say something like this:
>
> perl -MLWP::Simple -le '$c = get "http://3.am";print ref $c ? ref $c :
> "SCALAR VALUE"'


Jeff's original post is very misleading. The use of LWP is ineffectual and
his code shows nothing about the contents of the scalar variable, only the
fact that it is, indeed, a scalar. It is equivalent to:

$ perl -e 'print ref \$c'

which also prints 'SCALAR'. Hooray!

Rob
Chas Owens

2007-09-17, 7:00 pm

On 9/17/07, Wagner, David --- Senior Programmer Analyst --- WGO
snip
>
> $. == 400 and not $. = 400 ( assignment verses equality test ).
> Wags ;)

snip

Yeah, I am an idiot. That is what happens when I write code without testing it.
Chas Owens

2007-09-17, 7:00 pm

On 9/17/07, Rob Dixon <rob.dixon@350.com> wrote:
> Chas Owens wrote:
>
> Jeff's original post is very misleading. The use of LWP is ineffectual and
> his code shows nothing about the contents of the scalar variable, only the
> fact that it is, indeed, a scalar. It is equivalent to:
>
> $ perl -e 'print ref \$c'
>
> which also prints 'SCALAR'. Hooray!


It isn't completely ineffectual, just misleading. If LWP::Simple::get
had returned an arrayref then it would have said printed REF instead
of SCALAR:

perl -le '$c=[];print ref \$c'
REF

The problem is that it says the same thing for a hashref:

perl -le '$c={};print ref \$c'
REF

or even a scalarref

perl -le '$c=\$c;print ref \$c'
REF
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com