Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello Fellow Monks,

Long time since I've posted, lo and behold this time I have a question dealing with an actual program I've been asked to write, rather than an exercise. I'm writing a parser to take any random web page, and deaden all the links. This is the code:

#!usr/bin/perl use strict; my $file = "wiki.html"; my $file2 = "link_out_1.pl"; my $regex = "href\s*=\s*\"?.*\""; my $sub = "href=\"#\""; my $match; my $before; my $after; my $line; open(IN, "$file") || die $!; open(OUT, ">$file2") || die $!; while (<IN>) { chomp($line = $_); if ($line =~ /$regex/) { print "Got a Match\n"; $before = $`; $match = $&; $after = $'; $line =~ s/href\s*=\s*\"?.*\"/$sub/g; print OUT $line . $after . "\n"; #print OUT $' . "\n"; } else { # do nothing } # end if } # end while close(IN); close(OUT);

On this line:

$line =~ s/href\s*=\s*\"?.*\"/$sub/g;
I originally had
$line =~ s/$match/$sub/g;
But that would leave certain links unchanged. Mind you, my original regex catches everything I'm looking for, I think, however this substitution operator was my problem, and I ended up having to put the original expression in place of the variable that contained the match. Theoretically this makes no sense to me, and I'm thinking if I understood the reason behind this I might better understand the problem I had.

Also, please note, I saved the before and after of all the matches for later code I need to put together, not just to waste memory ;). Anyway, can anyone explain why I couldn't use $match in the line I pointed out? I'm not trying to get anyone to do my work for me, I'm only trying to understand what the problem was I just created a hack to fix.

Thanks :)

UPDATE:Thanks everyone for your very helpful replies, through which I have gained some great resources both for this project and future parsing projects. I would like to make one quick point, however, it turns out that this really is just a quick "one-off" (which YourMother pointed out as a slight possibility), so it wasn't really necessary to be dealing with a module, it isn't a program I'm planning to maintain for a long period of time, just something I wanted to be able to do quickly for a live application demo, something I could verify easily with my own two eyes, and also something that while perhaps better done with an alternative method, it is certainly enhancing my Perl skills, programming skills and introducing me to the world of parsing, therefore being more beneficial for me, in the long run, to do it in this manner, rather than those mentioned, IMHO, of course.

That being said, I am nonetheless planning to go and explore all of the modules mentioned by my fellow monks, momentarily much farther advanced in the Force (or rather, the Source) than myself, expanding the learning aspect of this project to even greater limits, so as I said in the beginning, Thank You very much to all of the monks who have taken their very precious time and effort to offer me a push in the right direction ;) ( also, fixed a typo )


In reply to Link Parser, something to be desired? by koolgirl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-25 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found