COULD you examine my source?

J

Jang

Hi, Experts!

I just wrote short Perl script aiming at the processes below

step 1) read "20.xml" in my C:\perl folder
step 2) extract links in "20.xml"
e.g. extracting "Link A, B, C,..." from 20.xml
<permalink> Link A </permalink>
....
<permalink> Link B </permalink>
....
<permalink> Link C </permalink>
....
step 3) save links at "20.txt"

Below is what I have written until now. I'm lost what line should be
added, what should be corrected...

=================================================

#!/usr/bin/perl -w

use strict;
use LWP::Simple;
use XML::TokeParser;
use XML::Simple;

my $t;
my $cont = get("20.xml");
my $p = new XML::TokeParser(\$cont);
$t = $p->get_tag("permalink");
$t = $p->get_trimmed_text("/permalink");

getstore($t, "20.txt");

=====================================================

Thank you for your help!

Jang.
 
I

Ian Wilson

Jang said:
Hi, Experts!

I just wrote short Perl script aiming at the processes below

step 1) read "20.xml" in my C:\perl folder
step 2) extract links in "20.xml"
e.g. extracting "Link A, B, C,..." from 20.xml
<permalink> Link A </permalink>
...
<permalink> Link B </permalink>
...
<permalink> Link C </permalink>
...
step 3) save links at "20.txt"

Below is what I have written until now. I'm lost what line should be
added, what should be corrected...

=================================================

#!/usr/bin/perl -w

use strict;

It is advisable to have "use warnings;" instead of -w but YMMV.
use LWP::Simple;

You dont seem to be using LWP, I'd omit the "use" until you are.
use XML::TokeParser;
use XML::Simple;

my $t;

Its advisable to not "declare" variables before they are used - see below.
my $cont = get("20.xml");

AFAIK you cant just throw in subroutine names like that. The first few
lines of documentation for XML::TokeParser say

#parse from file
my $p = XML::TokeParser->new('file.xml')

Why don't you use that?
my $p = new XML::TokeParser(\$cont);

I think you may be mixing up several different ways to feed XML data
into TokeParser. Pick one and use it consistently.
$t = $p->get_tag("permalink");

1) See above
my $token = $p->get_tag("permalink");
2) Why assign a value to $t if you never use it?
$t = $p->get_trimmed_text("/permalink");

You seem to be re-using $t for something different (text not token)
The documentation suggests.
$text = $p->get_trimmed_text($token);
getstore($t, "20.txt");

I'm not sure what this is. I'd use the normal open, print, close.

Caveat: I've not tried any of the above and have no prior experience
with TokeParser.
 
I

Ian Wilson

Tad said:
Seems he _is_ using LWP::Simple::get()...

Thanks for pointing that out. Silly of me not to check.

So far as I can tell, "20.txt" is not a valid URL for LWP::Simple's
get(). It doesn't seem to grok "file://20.txt" either.

Perhaps the OP intends to later develop the script to fetch XML from a
webserver, in which case use of LWP::Simple would be appropriate.

Meantime, I'd use open() and read the file in the usual way.

It is LWP::Simple::getstore().

Thanks Tad. In which case the OP seems to be barking up the wrong tree:

getstore($url, $file)
Gets a document identified by a URL and stores it in the file.
The return value is the HTTP response code.

$t doesn't contain a URL. And the OP probably wants to simply write $t
to a text file (for which I'd use open, print, close).
 

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

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top